成功登录后重定向或注册在下一个身份验证中输入凭据

是否有任何示例和/或方法可以在成功登录或注册输入凭据时重定向到私人仪表板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)

  }

}


子衿沉夜
浏览 89回答 3
3回答

慕码人2483693

这实际上可能在启动登录时发生。从文档中,您可以将回调 URL 传递给登录。您的代码将如下所示。signIn(provider.id, {       callbackUrl: `${window.location.origin}/protected`,     })

胡子哥哥

使用新版本的 Next.js,您可以对“getStaticProps”方法进行重定向,如下所示,export async function getStaticProps(context) {  // some imperative work..  //  if (!user) {    return {      redirect: {        destination: '/', // some destination '/dashboard' Ex,        permanent: false,      },    }  }  return {    props: {},  }}

波斯汪

它对我来说是这样的"use client";import Link from "next/link";import { signIn } from "next-auth/react";import { useState } from "react";import { toast } from "react-toastify";const Login = () => {&nbsp; const [email, setEmail] = useState("");&nbsp; const [password, setPassword] = useState("");&nbsp; // handle submit event&nbsp; const handleSubmit = async (e) => {&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; const isLoggedin = await signIn(&nbsp; &nbsp; &nbsp; &nbsp; "credentials",&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; { callbackUrl: "/dashboard/profile" }&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; if (isLoggedin.error !== null) {&nbsp; &nbsp; &nbsp; &nbsp; toast.error("Incorrect Login Details!!");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; toast.success("Login Successful!!");&nbsp; &nbsp; &nbsp; &nbsp; // router.replace("/dashboard/profile");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (error) {&nbsp; &nbsp; &nbsp; toast.success(error);&nbsp; &nbsp; }&nbsp; };&nbsp; return (&nbsp; &nbsp; <div&nbsp; &nbsp; &nbsp; style={{ maxWidth: "480px" }}&nbsp; &nbsp; &nbsp; className="mt-10 mb-20 p-4 md:p-7 mx-auto rounded bg-white shadow-lg"&nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; <form onSubmit={handleSubmit}>&nbsp; &nbsp; &nbsp; &nbsp; <h2 className="mb-5 text-2xl font-semibold">Login</h2>&nbsp; &nbsp; &nbsp; &nbsp; <div className="mb-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label className="block mb-1"> Email </label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="Type your email"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={(e) => setEmail(e.target.value)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div className="mb-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label className="block mb-1"> Password </label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="password"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="Type your password"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minLength={6}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={(e) => setPassword(e.target.value)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="submit"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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"&nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Login&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp;&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; );};export default Login;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript