@@ -1,5 +1,4 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit' -import { createAction } from '@reduxjs/toolkit/src' import axios, { AxiosResponse } from 'axios' import { loadingThunk } from '#/app/store/store' @@ -119,39 +118,9 @@ export const userSlice = createSlice({ }, extraReducers: (builder) => { builder.addCase(getAllInfo.fulfilled, (state, action) => { + Object.assign(state, action.payload) state.status = 'succeeded' state.referral = '' - const { - uid, - is_subscribed_to_emails, - email, - account_type, - first_name, - last_name, - profile_picture_link, - is_active, - is_staff, - created_at, - payment_plan, - is_confirmed, - is_social, - referral_code, - show_balance, - } = action.payload - state.is_subscribed_to_emails = is_subscribed_to_emails - state.account_type = account_type - state.email = email - state.payment_plan = payment_plan - state.is_confirmed = is_confirmed - state.first_name = first_name - state.last_name = last_name - state.profile_picture_link = profile_picture_link - state.is_active = is_active - state.is_staff = is_staff - state.created_at = created_at - state.is_social = is_social - state.referral_code = referral_code - state.show_balance = show_balance }) builder.addCase(getAllInfo.pending, (state) => { state.status = 'pending' @@ -159,7 +128,7 @@ export const userSlice = createSlice({ builder.addCase(getAllInfo.rejected, (state) => { state.status = 'failed' }) - builder.addCase(unfollowEmail.fulfilled, (state, action) => { + builder.addCase(unfollowEmail.fulfilled, (state) => { state.is_subscribed_to_emails = !state.is_subscribed_to_emails }) }, @@ -74,10 +74,7 @@ export const authOptions: NextAuthOptions = { throw new Error('Непредвиденная ошибка') } - const info = await getAll( - (result.data as { access_token: string }).access_token - ) - + const info = await getAll((result.data as { access_token: string }).access_token) return { ...info.token, tokenExpiry: AuthConstants.getExpiresDate() } } @@ -87,12 +84,12 @@ export const authOptions: NextAuthOptions = { token.refresh = user.token.refresh token.tokenExpiry = AuthConstants.getExpiresDate() return token - } catch (error) { + } catch { return { ...token, ...user } } } - const shouldRefreshTime = Math.round(Date.now() - token.tokenExpiry) >= 0 + const shouldRefreshTime = Math.round(Date.now() - (token.tokenExpiry as number)) >= 0 if (shouldRefreshTime) { return await AuthProxy.refreshToken(token) @@ -124,6 +121,7 @@ export const authOptions: NextAuthOptions = { async session({ session, token }) { session.access = token.access session.refresh = token.refresh + session.error = token.error return session }, }, @@ -15,10 +15,8 @@ export abstract class AuthConstants { public static readonly sessionTime = Number(process.env.SESSION_TIME) - /* Время в часах через которое обновляется access токен **/ - // private static readonly expiresTime = 0.5 - // private static readonly expiresTime = 0.002778 - private static readonly expiresTime = this.sessionTime * 1000 // 5 минут + + private static readonly expiresTime = this.sessionTime public static readonly getExpiresDate = () => Date.now() + this.expiresTime } @@ -9,22 +9,15 @@ export class AuthorizationProxy { public readonly loginByEmail = async (email: string, password: string) => { return await fetch(API_URL + '/auth/login', { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - email, - password, - }), + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }), }) } public readonly loginByEmailToken = async (emailToken: string) => { return await fetch(API_URL + `/auth/login-from-token/${emailToken}`, { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, + headers: { 'Content-Type': 'application/json' }, }) } @@ -40,10 +33,6 @@ export class AuthorizationProxy { }, { validateStatus: () => true } ) - - // const finalData = await getAll(data.access_token) - - // return finalData.token } public readonly exchangeTokenYandexTest = async (yandex_token: string) => { @@ -60,15 +49,18 @@ export class AuthorizationProxy { public readonly refreshToken = async (tokenObject: JWT): Promise => { try { - const { data } = await axios.post(API_URL + '/auth/token/refresh', { - refresh: tokenObject.refresh, - }) + const { data } = await axios.post<{ access?: string; refresh?: string }>( + API_URL + '/auth/refresh', + { refresh: tokenObject.refresh } + ) return { - access: data.access, - refresh: data.refresh, + ...tokenObject, + access: data.access!, + refresh: data.refresh ?? tokenObject.refresh, tokenExpiry: AuthConstants.getExpiresDate(), + error: undefined, } - } catch (error) { + } catch { return { ...tokenObject, error: 'RefreshAccessTokenError', @@ -13,6 +13,7 @@ declare module 'next-auth' { expires: ISODateString access: string refresh: string + error?: 'RefreshAccessTokenError' } interface User { @@ -30,5 +31,6 @@ declare module 'next-auth/jwt' { access: string refresh: string tokenExpiry: number + error?: 'RefreshAccessTokenError' } }