@@ -6,7 +6,6 @@ import { AuthConstants, ERROR_MAPPING } from './constants' import { AuthorizationProxy } from './proxy' import * as Sentry from '@sentry/nextjs' -import { getAll } from '#/entities/user-account/model/user-type-slice' const AuthProxy = new AuthorizationProxy() @@ -30,20 +29,28 @@ export const authOptions: NextAuthOptions = { const resp = await AuthProxy.loginByEmail(username, password) const data = await resp.json() if (resp.status >= 400 && resp.status < 500) { - throw new Error( - ERROR_MAPPING[(data as { detail: string }).detail] ?? - JSON.stringify(data as Object) - .replace(/\s+/, '_') - .replace(/[А-Яа-я]/, '') - .toLowerCase() - ) + throw new Error(ERROR_MAPPING[(data as { detail: string }).detail] + ?? JSON.stringify(data as Object) + .replace(/\s+/, '_') + .replace(/[А-Яа-я]/, '').toLowerCase()) } - return (await data) as User + return await data as User }, }), ], callbacks: { async jwt({ token, user, account }) { + if (account?.provider === 'yandex') { + const result = await AuthProxy.exchangeTokenYandex(account.access_token || '') + + const { access, refresh } = result + + token.access = access + token.refresh = refresh + token.tokenExpiry = AuthConstants.getExpiresDate() + return token + } + if (user) { try { token.access = user.token.access @@ -63,42 +70,6 @@ export const authOptions: NextAuthOptions = { return token }, - signIn: async ({ user, account, ...props }) => { - if (account?.provider === 'yandex') { - const { data: result, status } = await AuthProxy.exchangeTokenYandex( - account.access_token! - ) - - if (status >= 400 && status < 500) { - return Promise.reject( - new Error((result as { error_description: string }).error_description) - ) - } - - return Promise.resolve(true) - - // if (status >= 400 && status < 500) { - // console.log(result) - // throw new Error( - // ERROR_MAPPING[(result as { detail: string }).detail] ?? - // JSON.stringify(result as Object) - // .replace(/\s+/, '_') - // .replace(/[А-Яа-я]/, '') - // .toLowerCase() - // ) - - // return - // } - - // const data = result as { access_token: string } - - // const finalData = await getAll(data.access_token) - - // const { access, refresh } = finalData.token - } - - return Promise.resolve(true) - }, async session({ session, token }) { session.access = token.access session.refresh = token.refresh @@ -108,7 +79,7 @@ export const authOptions: NextAuthOptions = { pages: { signIn: '/login', error: '/login', - }, + } } export default NextAuth(authOptions) @@ -1,7 +1,7 @@ import axios, { AxiosResponse } from 'axios' import { User } from 'next-auth' import { JWT } from 'next-auth/jwt' -import * as Sentry from '@sentry/browser' +import * as Sentry from "@sentry/browser"; import { getAll } from '#/entities/user-account/model/user-type-slice' import { API_URL } from '#/shared/lib/constants' @@ -17,33 +17,27 @@ export class AuthorizationProxy { return await fetch(API_URL + '/auth/login', { method: 'POST', headers: { - 'Content-Type': 'application/json', + 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password, - }), + }) }) } - public readonly exchangeTokenYandex = async (yandex_token: string) => { - return await axios.post< - | { access_token: string } - | { - error: string - error_description: string - } - >( - API_URL + '/auth/login-social/convert-token', - { - grant_type: 'convert_token', - client_id: AuthConstants.django_app_client_id_yandex, - client_secret: AuthConstants.django_app_client_secret_yandex, - backend: 'yandex-oauth2', - token: yandex_token, - }, - { validateStatus: (status) => true } - ) + public readonly exchangeTokenYandex = async (yandex_token: string): Promise => { + const { data } = await axios.post(API_URL + '/auth/login-social/convert-token', { + grant_type: 'convert_token', + client_id: AuthConstants.django_app_client_id_yandex, + client_secret: AuthConstants.django_app_client_secret_yandex, + backend: 'yandex-oauth2', + token: yandex_token, + }) + + const finalData = await getAll(data.access_token) + + return finalData.token } public readonly refreshToken = async (tokenObject: JWT): Promise => {