Using an external API as a Credentials provider brings me no user. #4108
Unanswered
ntalamdotcom
asked this question in
Help
Replies: 1 comment
-
I lost my 2 days, I did not figure out the flow with the external express backend, and now I am thinking to back on react. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I lost an entire day trying to understand how to login using a custom endpoint that uses a username and a password.
It brings me nothing but a headache.
the result of this is just an empty Json impossible to transform.
`import NextAuth from "next-auth"
import GithubProvider from "next-auth/providers/github"
import CredentialsProvider from "next-auth/providers/credentials"
export default NextAuth({
// Configure one or more authentication providers
providers: [
/* GithubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),/
CredentialsProvider({
// The name to display on the sign in form (e.g. 'Sign in with...')
name: 'XXXXXXXXXXXX',
// The credentials is used to generate a suitable form on the sign in page.
// You can specify whatever fields you are expecting to be submitted.
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the tag through the object.
credentials: {
username: { label: "Username", type: "text", placeholder: "jsmith" },
password: { label: "Password", type: "password" }
},
/async session({ session, token }) {
session.user = token.user;
return session;
},
async jwt({ token, user }) {
if (user) {
token.user = user;
}
return token;
},/
async authorize(credentials, req) {
// You need to provide your own logic here that takes the credentials
// submitted and returns either a object representing a user or value
// that is false/null if the credentials are invalid.
// e.g. return { id: 1, name: 'J Smith', email: '[email protected]' }
// You can also use the req object to obtain additional parameters
// (i.e., the request IP address)
console.log("SON OF A XXXXXXX: !!+" + JSON.stringify(credentials))
var user = {
"id": "3",
"username": "XXXXXXXX",
"first_name": "XXXXXXXX",
"last_name": "XXXXXXXX",
"email": "XXXXXXXXXXXXXX",
"active_reg": true,
"password": "XXXXXXXXX",
"date_created": "2022-02-25T07:22:09.387Z",
"last_login": null,
"status": 200
}
//try {
const res = await fetch("https://XXXXXXXXXXXXXXXXXXX.amazonaws.com/headerstest/users/gets/login", {
method: 'GET',
//body: JSON.stringify(credentials),
headers: {
"Content-Type": "application/json",
"x-api-key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"user_name": credentials.username,
"pass_word": credentials.password,
}
})
console.log("Req: " + JSON.stringify(req))
console.log("res: " + JSON.stringify(res))
user = await res.json()
//const user = await JSON.stringify(res)
console.log("Termatrac: " + JSON.stringify(user))
// If no error and we have user data, return it
if (res.ok && user) {
return user
}
if (!res.ok) {
throw new Error(user.exception);
}
// If no error and we have user data, return it
if (res.ok && user) {
return user;
}
// Return null if user data could not be retrieved
/} catch (error) {
console.log("error: "+error)
//return error
}*/
return null
}
})
],
pages: {
error: '/_error', // Error code passed in query string as ?error=
}
})`
The content of the response is : res: {"size":0,"timeout":0}
Please help me getting my user data
Beta Was this translation helpful? Give feedback.
All reactions