Skip to content

Instantly share code, notes, and snippets.

@FeMaffezzolli
Created May 3, 2023 04:30
Show Gist options
  • Save FeMaffezzolli/706e27b24ef8d8287b242498b716a580 to your computer and use it in GitHub Desktop.
Save FeMaffezzolli/706e27b24ef8d8287b242498b716a580 to your computer and use it in GitHub Desktop.
next auth
import NextAuth, { NextAuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import { serverClient } from '../../../server/client'
type AuthResponse = {
access_token: string
expires_in: number
token_type: string
scope: string
refresh_token: string
}
type ContaResponse = {
mensagem: 'string',
sucesso: true,
conteudo: {
id: 'string',
name: 'string',
email: '[email protected]',
password: 'string',
consorciado: {
logradouro: 'string',
numero: 'string',
complemento: 'string',
bairro: 'string',
cep: 'string',
cidade: 'string',
estado: 'st',
dataRegistro: '2022-11-22T15:31:30.236Z',
dataNascimento: '2022-11-22T15:31:30.236Z',
tipoPessoa: 'Fisica',
cpfCnpj: 'string',
telefone: 'string',
celular: 'string',
formatoChaveDICT: 'Email',
chavePix: 'string'
},
representante: true
}
}
export const authOptions: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
// @ts-ignore
site: process.env.NEXTAUTH_URL,
providers: [
CredentialsProvider({
name: 'CredentialsProvider',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' }
},
type: 'credentials',
id: 'credentials',
async authorize (credentials) {
// Add logic here to look up the user from the credentials supplied
if (!credentials || !credentials.email || !credentials.password) {
return null
}
const options = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
grant_type: 'password',
username: credentials.email,
password: credentials.password,
client_id: 'hubxp',
client_secret: 'Hg452*d@s'
}
}
try {
const authResponse = await serverClient.post<AuthResponse>(
'/Auth',
options.data,
{
headers: options.headers
}
)
const contaResponse = await serverClient.get<ContaResponse>('/Conta/MinhaConta', {
headers: {
Authorization: `${authResponse.data.token_type} ${authResponse.data.access_token}`
}
})
const {
data: {
conteudo,
mensagem,
sucesso
}
} = contaResponse
if (!sucesso) {
throw new Error(mensagem)
}
const firstName = conteudo.name.split(' ')[0]
const lastName = conteudo.name.split(' ')[1]
return {
email: conteudo.email,
name: conteudo.name,
image: `https://ui-avatars.com/api/?name=${firstName}+${lastName}&background=222222&color=fff`,
id: conteudo.id,
accessToken: authResponse.data.access_token
}
} catch (error) {
console.error('Erro na autenticação. Código: 100', error)
return null
}
}
})
],
callbacks: {
signIn (params) {
console.log('@signIn', params)
return true
},
jwt (params) {
console.log('@jwt', params)
const { user, token } = params
if (user) {
// @ts-ignore
token.accessToken = user.accessToken
token.userId = user.id
}
return token
},
session (params) {
console.log('@session', params)
const { token, session } = params
if (token.accessToken) {
// @ts-ignore
session.accessToken = token.accessToken
}
if (token.userId) {
// @ts-ignore
session.userId = token.userId
}
return session
}
},
debug: true
}
export default NextAuth(authOptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment