Replies: 21 comments 26 replies
-
Use the 5.0.0-beta.3 version is working there. |
Beta Was this translation helpful? Give feedback.
-
Sorry, no updates yet,
I'm getting only "CallbackRouteError" as well
Greetings Petar
…On Wed, Jan 24, 2024 at 3:25 PM Adeyanju Adeyemi ***@***.***> wrote:
Please is there any new update on this ??.
Throwing an error like this used to work in v4:
CredentialsProvider({
async authorize(credentials: any) {
let urlencoded = new URLSearchParams();
urlencoded.append('email', credentials.email);
urlencoded.append('password', credentials.password);
const res = await AuthApiInstance.signIn<UserSchemaType>(urlencoded);
console.log(res);
if (res.isError && !res.resData) {
throw new Error(JSON.stringify({ errors: res.error, status: res.statusCode }))
};
if (res.resData?.data.token) {
encryptCookieDataAction(res.resData?.data.token, EncryptionCookiesKeys.AdminToken)
}
return res.resData?.data as any
},
credentials: {},
})
Now I am only getting "CallbackRouteError", any help will be appreciated
—
Reply to this email directly, view it on GitHub
<#8999 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ASNW6Z3MRQQDUMSRMDUYIKDYQEDWJAVCNFSM6AAAAAA6VNAMZSVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DEMZSGYZDG>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Same issue here with 5.0.0-beta.5. One possible workaround would be to use a server action to handle error logic in conjunction with the server-side signIn(). However, there seems to be an issue with the server-side signIn() not syncing with the client-side SessionProvider/useSession() such that user logs out status will be 'unauthenticated'. New user signs in, status still is authenticated (until page refresh). |
Beta Was this translation helpful? Give feedback.
-
I pushed out a PR for this. See #9801 |
Beta Was this translation helpful? Give feedback.
-
Hi all! I want to support custom errors, please check out my proposal at #9099 (comment) and leave a comment! Note. If you want to agree/disagree only, just 👍 or 👎. Only comment, if you have a meaningful suggestion to keep it effective! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Working with "next-auth": "4.24.4" and it's already working with the Error class.
|
Beta Was this translation helpful? Give feedback.
-
I was facing a problem where signIn wasn't working as expected in login forms with version "use server"
import { DEFAULT_LOGIN_REDIRECT } from "@/routes"
import { AuthError } from "next-auth"
import { signIn } from "@/lib/auth"
export const login = async (phone: string, code: string, callbackUrl?: string | null) => {
try {
await signIn("phone", { phone, code, redirectTo: callbackUrl || DEFAULT_LOGIN_REDIRECT })
} catch (error) {
console.error("phone login: ", error)
if (error instanceof AuthError) {
return { error: "error", message: error.message, status: 401 }
}
throw error
}
} |
Beta Was this translation helpful? Give feedback.
-
just return Promise.reject(new Error(" CUISTOMER MESSAGE ")); |
Beta Was this translation helpful? Give feedback.
-
If we throw a error inside authorize function how can we show it in the client side in the credential provider |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
still issue |
Beta Was this translation helpful? Give feedback.
-
still issue |
Beta Was this translation helpful? Give feedback.
-
I HATE NEXT AUTH |
Beta Was this translation helpful? Give feedback.
-
still issue |
Beta Was this translation helpful? Give feedback.
-
stille issue |
Beta Was this translation helpful? Give feedback.
-
I am experiencing the same thing as well. For now my solution is as follows in my signIn callback: Try{
throw new Error('Invalid sign up session')
} catch (error) {
if (error instanceof Error) {
return `/auth/error?error=${error.message}`
}
} |
Beta Was this translation helpful? Give feedback.
-
still issue 😠 |
Beta Was this translation helpful? Give feedback.
-
I have tried to throw the error a several places but the response always retuned successfully, and the response is always a url to the error page. So i decided to return it as successful from the This is how i retrieve the error properly
and in the callbacks
In the UI code
please checkout this answer: https://stackoverflow.com/a/76761731/11762861 |
Beta Was this translation helpful? Give feedback.
-
From the documentation it's not clear enough. After a day of trying different workarounds I understood one thing. DON'T use signIn function from I tried the following: In import NextAuth, {CredentialsSignin} from "next-auth"
import Credentials from "next-auth/providers/credentials"
import {login} from "@/services/api/auth";
import {AuthConfig} from "@auth/core";
export class InvalidLoginError extends CredentialsSignin {
code = 'invalid_credentials'
}
export const authConfig: AuthConfig = {
providers: [
Credentials({
credentials: {
username: {},
password: {},
},
authorize: async (credentials): Promise<{name: string, accessToken: string}> => {
const username = credentials.username as string
const password = credentials.password as string
try {
let loginResponse = await login(username, password)
return {
name: password,
accessToken: loginResponse.data.access_token
}
} catch (e: any) {
throw new InvalidLoginError(e.json.detail)
}
},
}),
],
}
export const { handlers, signIn, signOut, auth } = NextAuth(authConfig) Remove 'use server'
import {InvalidLoginError, signIn} from "@/auth";
export async function authenticate(email: string, password: string) {
try {
const r = await signIn("credentials", {
username: email,
password: password,
callbackUrl: "/",
redirect: false,
})
return r
} catch (error) {
if (error.cause.err instanceof InvalidLoginError) {
return {"error": "Incorrect username or password"}
} else {
throw new Error("Failed to authenticate")
}
}
} Now you are good to use this After searching a bit in the source code, I found that there is room for this. Here’s auth.js throwing the exact error if it's an instance of This means if you extend your custom error from export class InvalidLoginError extends AuthError {
code = 'invalid_credentials' // code is not member of AuthError but does not matter
} You will be able to handle it without looking through export class InvalidLoginError extends AuthError {
code = 'invalid_credentials'
constructor(message: string) {
super(message)
this.code = message
}
} and use it for all types of custom errors: try {
// ...
} catch (e: any) {
throw new InvalidLoginError("invalid_credentials")
} And in server actions, check it: try {
// ...
} catch (e: any) {
if (e.code === "invalid_credentials") {
return {error: "Incorrect username or password"}
} else {
throw Error("Failed to authenticate")
}
} |
Beta Was this translation helpful? Give feedback.
-
This works like a charm. |
Beta Was this translation helpful? Give feedback.
-
Goals
Throw custom error message or status code from authorize() instead of "CallbackRouteError" and status 200.
Sign In process (when credentials are correct) sometimes errors with "User not confirmed" in my backend and I would like to return a custom error message OR at least a status code to the front-end so I can render a UI to enter confirmation code. Sign In is called from next-auth/react with redirect:false.
Background
Our company's online platform has been using Next-Auth v4 with Credentials Provider and a custom authorize() function that initiates an AWS Cognito userauth and returns the tokens for jwt callback to handle and organize the data.
Our Sign In page will automatically render a UI for the user to enter confirmation code once they attempt to sign in and Cognito returns an error stating "User not confirmed.". We catch this error within authorize() and throw it to the front-end so our app can render the respective UI.
However, now with v5, the ONLY error message I get to the front end says "CallbackRouteError" with a status code of 200 OK. There is no way even to throw a custom error status code to the front end.
Proposal
This is specific to Next-Auth/Authjs v5.
In Credentials Provider, please allow us to throw a custom error message or a status code in the return value from the signIn function when redirect is set to false.
We require custom errors like "User not confirmed", "Invalid credentials", or "User suspended.".
Or let us throw integer error codes in the status and render a UI on the front-end based on these codes.
Beta Was this translation helpful? Give feedback.
All reactions