feat: align auth api with front convention (#370)

* feat: align auth api with front convention

* fix: email password auth

* fix: proper file naming

* Fix login

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Jérémy M
2023-06-24 07:43:54 +02:00
committed by GitHub
parent c6708b2c1f
commit 31145c5518
23 changed files with 222 additions and 293 deletions

View File

@ -5,17 +5,14 @@ import {
InMemoryCache,
UriFunction,
} from '@apollo/client';
import jwt from 'jwt-decode';
import { cookieStorage } from '@/utils/cookie-storage';
import { loggerLink } from '@/utils/apollo-logger';
import {
AuthTokenPair,
RenewTokenDocument,
RenewTokenMutation,
RenewTokenMutationVariables,
} from '~/generated/graphql';
import { loggerLink } from '~/providers/apollo/logger';
import { tokenService } from './TokenService';
const logger = loggerLink(() => 'Twenty-Refresh');
@ -60,29 +57,15 @@ const renewTokenMutation = async (
* @param uri string | UriFunction | undefined
* @returns TokenPair
*/
export const renewToken = async (uri: string | UriFunction | undefined) => {
const tokenPair = tokenService.getTokenPair();
export const renewToken = async (
uri: string | UriFunction | undefined,
tokenPair: AuthTokenPair | undefined | null,
) => {
if (!tokenPair) {
throw new Error('Refresh token is not defined');
}
const data = await renewTokenMutation(uri, tokenPair.refreshToken);
const data = await renewTokenMutation(uri, tokenPair.refreshToken.token);
tokenService.setTokenPair(data.renewToken.tokens);
return data.renewToken;
};
export const getUserIdFromToken: () => string | null = () => {
const accessToken = cookieStorage.getItem('accessToken');
if (!accessToken) {
return null;
}
try {
return jwt<{ sub: string }>(accessToken).sub;
} catch (error) {
return null;
}
return data.renewToken.tokens;
};