Last active
October 15, 2024 13:10
-
-
Save MohammadaliMirhamed/8c93637775ea6303532569da607f45ba to your computer and use it in GitHub Desktop.
my ts file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CredentialsProvider from "next-auth/providers/credentials"; | |
import { NuxtAuthHandler } from "#auth"; | |
export default NuxtAuthHandler({ | |
secret: useRuntimeConfig().authSecret, | |
providers: [ | |
CredentialsProvider.default({ | |
name: "Credentials", | |
async authorize(credentials: any) { | |
try { | |
const response = await $fetch(`${useRuntimeConfig().public.apiUrl}/api/v1/auth/login/`, | |
{ | |
method: "POST", | |
body: JSON.stringify({ | |
email: credentials.email, | |
password: credentials.password, | |
}), | |
} | |
); | |
return response | |
} catch (e) { | |
throw createError({ | |
statusCode: 403, | |
statusMessage: "Credentials not working", | |
}); | |
} | |
}, | |
}), | |
], | |
callbacks: { | |
jwt: ({ token, user }) => { | |
if (user) { | |
token.user = { | |
uuid: user.user.uuid, | |
first_name: user.user.first_name, | |
last_name: user.user.last_name, | |
phone_number: user.user.phone_number, | |
email: user.user.email, | |
} | |
token.tokens = { | |
access_token: user.tokens.access_token, | |
refresh_token: user.tokens.refresh_token, | |
} | |
} | |
return Promise.resolve(token) | |
}, | |
session: ({ session, token }) => { | |
(session as any).user = { | |
uuid: (token as any).user.uuid, | |
first_name: (token as any).user.first_name, | |
last_name: (token as any).user.last_name, | |
phone_number: (token as any).user.phone_number, | |
email: (token as any).user.email | |
}; | |
(session as any).tokens = { | |
access_token: (token as any).tokens.access_token, | |
refresh_token: (token as any).tokens.refresh_token, | |
}; | |
return Promise.resolve(session) | |
} | |
}, | |
pages: { | |
signIn: '/auth/login' | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment