亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

成功登錄后重定向或注冊在下一個身份驗證中輸入憑據

成功登錄后重定向或注冊在下一個身份驗證中輸入憑據

子衿沉夜 2023-09-14 22:07:53
是否有任何示例和/或方法可以在成功登錄或注冊輸入憑據時重定向到私人儀表板next-auth?我找不到任何關于此的明確文檔。我正在考慮在下面添加重定向,但不確定這是否是正確的方法:callbacks.signIn = async (data, account, profile) => {  if ((account.provider === 'google' && profile.verified_email === true) || (account.type === 'credentials' && data.status === 200)) {    return Promise.resolve(true)  } else {    return Promise.resolve(false)  }}
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

這實際上可能在啟動登錄時發生。從文檔中,您可以將回調 URL 傳遞給登錄。您的代碼將如下所示。

signIn(provider.id,?{
??????callbackUrl:?`${window.location.origin}/protected`,
????})


查看完整回答
反對 回復 2023-09-14
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

使用新版本的 Next.js,您可以對“getStaticProps”方法進行重定向,如下所示,


export async function getStaticProps(context) {

? // some imperative work..



? //

? if (!user) {

? ? return {

? ? ? redirect: {

? ? ? ? destination: '/', // some destination '/dashboard' Ex,

? ? ? ? permanent: false,

? ? ? },

? ? }

? }


? return {

? ? props: {},

? }

}


查看完整回答
反對 回復 2023-09-14
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

它對我來說是這樣的


"use client";

import Link from "next/link";

import { signIn } from "next-auth/react";

import { useState } from "react";

import { toast } from "react-toastify";


const Login = () => {

  const [email, setEmail] = useState("");

  const [password, setPassword] = useState("");


  // handle submit event

  const handleSubmit = async (e) => {

    e.preventDefault();


    try {

      const isLoggedin = await signIn(

        "credentials",

        {

          email,

          password,

        },

        { callbackUrl: "/dashboard/profile" }

      );


      if (isLoggedin.error !== null) {

        toast.error("Incorrect Login Details!!");

      } else {

        toast.success("Login Successful!!");

        // router.replace("/dashboard/profile");

      }

    } catch (error) {

      toast.success(error);

    }

  };


  return (

    <div

      style={{ maxWidth: "480px" }}

      className="mt-10 mb-20 p-4 md:p-7 mx-auto rounded bg-white shadow-lg"

    >

      <form onSubmit={handleSubmit}>

        <h2 className="mb-5 text-2xl font-semibold">Login</h2>


        <div className="mb-4">

          <label className="block mb-1"> Email </label>

          <input

            className="appearance-none border border-gray-200 bg-gray-100 rounded-md py-2 px-3 hover:border-gray-400 focus:outline-none focus:border-gray-400 w-full"

            type="text"

            placeholder="Type your email"

            required

            onChange={(e) => setEmail(e.target.value)}

          />

        </div>


        <div className="mb-4">

          <label className="block mb-1"> Password </label>

          <input

            className="appearance-none border border-gray-200 bg-gray-100 rounded-md py-2 px-3 hover:border-gray-400 focus:outline-none focus:border-gray-400 w-full"

            type="password"

            placeholder="Type your password"

            minLength={6}

            required

            onChange={(e) => setPassword(e.target.value)}

          />

        </div>


        <button

          type="submit"

          className="my-2 px-4 py-2 text-center w-full inline-block text-white bg-blue-600 border border-transparent rounded-md hover:bg-blue-700"

        >

          Login

        </button> 

      </form>

    </div>

  );

};


export default Login;


查看完整回答
反對 回復 2023-09-14
  • 3 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號