Auth provider added to the project
This commit is contained in:
@@ -18,7 +18,7 @@ const ACCESS_TOKEN_KEY: 'access_token' = 'access_token' as const;
|
||||
const REFRESH_TOKEN_KEY: 'refresh_token' = 'refresh_token' as const;
|
||||
const EXPIRES_IN_KEY: 'expires_in' = 'expires_in' as const;
|
||||
|
||||
export const generateToken = async (request: GenerateToken) => {
|
||||
export const generateTokenWithPassword = (request: GenerateToken) => {
|
||||
const body = new URLSearchParams();
|
||||
body.set('grant_type', 'password');
|
||||
body.set('client_id', import.meta.env.VITE_IDENTITY_CLIENT_ID);
|
||||
@@ -26,7 +26,7 @@ export const generateToken = async (request: GenerateToken) => {
|
||||
body.set('username', request.username);
|
||||
body.set('password', request.password);
|
||||
|
||||
const result = await apiClient.post<GenerateTokenResponse>(
|
||||
return apiClient.post<GenerateTokenResponse>(
|
||||
import.meta.env.VITE_IDENTITY_URL,
|
||||
body.toString(),
|
||||
{
|
||||
@@ -35,75 +35,4 @@ export const generateToken = async (request: GenerateToken) => {
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
sessionStorage.setItem(ACCESS_TOKEN_KEY, result.data.access_token);
|
||||
sessionStorage.setItem(REFRESH_TOKEN_KEY, result.data.refresh_token);
|
||||
sessionStorage.setItem(
|
||||
EXPIRES_IN_KEY,
|
||||
String(Date.now() + result.data.expires_in * 1000),
|
||||
);
|
||||
};
|
||||
|
||||
export const getAccessToken = () => {
|
||||
const sessionToken = sessionStorage.getItem(ACCESS_TOKEN_KEY);
|
||||
|
||||
if (sessionToken) return null;
|
||||
|
||||
const expiresAt = Number(sessionStorage.getItem(EXPIRES_IN_KEY));
|
||||
const now = Date.now();
|
||||
|
||||
if (now < expiresAt) {
|
||||
return sessionToken;
|
||||
} else {
|
||||
return refreshAccessToken();
|
||||
}
|
||||
};
|
||||
|
||||
const refreshAccessToken = async () => {
|
||||
const refreshToken = sessionStorage.getItem(REFRESH_TOKEN_KEY);
|
||||
if (!refreshToken) return null;
|
||||
|
||||
const result = await axios.post<GenerateTokenResponse>(
|
||||
import.meta.env.VITE_IDENTITY_URL,
|
||||
new URLSearchParams({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refreshToken,
|
||||
client_id: 'harmony_identity', // from your token payload
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (result.data.access_token) {
|
||||
sessionStorage.setItem(ACCESS_TOKEN_KEY, result.data.access_token);
|
||||
sessionStorage.setItem(
|
||||
EXPIRES_IN_KEY,
|
||||
String(Date.now() + result.data.expires_in * 1000),
|
||||
);
|
||||
return result.data.access_token;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
// export const generateToken = (request: any) => {
|
||||
// const body = new URLSearchParams();
|
||||
// body.set('grant_type', request.password ? 'password' : 'otp');
|
||||
// body.set('client_id', import.meta.env.VITE_IDENTITY_CLIENT_ID);
|
||||
// body.set('scope', import.meta.env.VITE_IDENTITY_SCOPE);
|
||||
// if (request.phoneNumber) body.set('phonenumber', request.phoneNumber);
|
||||
// if (request.email) body.set('email', request.email);
|
||||
// if (request.password) body.set('password', request.password);
|
||||
// if (request.otp) body.set('otp', request.otp);
|
||||
|
||||
// return axios
|
||||
// .post(import.meta.env.VITE_IDENTITY_URL, body.toString(), {
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
// },
|
||||
// })
|
||||
// .then((result) => console.log(result));
|
||||
// };
|
||||
|
||||
@@ -62,7 +62,7 @@ export const AuthenticationSteps = (): JSX.Element => {
|
||||
};
|
||||
|
||||
const handlePhoneNumberVerified = () => {
|
||||
redirectToReturnUrl();
|
||||
navigate('/signup');
|
||||
};
|
||||
|
||||
const redirectToReturnUrl = () => {
|
||||
|
||||
@@ -21,7 +21,8 @@ import {
|
||||
import type { PasswordLoginRequest } from '../../types/userTypes';
|
||||
import { Icon, useToast } from '@rkheftan/harmony-ui';
|
||||
import { useApi } from '@/hooks/useApi';
|
||||
import { generateToken } from '../../api/identityAPI';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { generateTokenWithPassword } from '../../api/identityAPI';
|
||||
|
||||
export interface EnterPasswordFormProps {
|
||||
onEditValue: () => void;
|
||||
@@ -59,6 +60,7 @@ export const EnterPasswordForm = ({
|
||||
loading: loginWithPassLoading,
|
||||
execute: loginWithPassCall,
|
||||
} = useApi(loginWithPassword);
|
||||
const auth = useAuth();
|
||||
|
||||
const handleBlur = () => {
|
||||
setInputTouched(true);
|
||||
@@ -80,12 +82,13 @@ export const EnterPasswordForm = ({
|
||||
if (!loginWithPassResult) return;
|
||||
|
||||
if (loginWithPassResult.success) {
|
||||
console.log('logged in');
|
||||
|
||||
await generateToken({
|
||||
const tokenRes = await generateTokenWithPassword({
|
||||
username: apiRequest.email ?? (apiRequest.phoneNumber as string),
|
||||
password: apiRequest.password,
|
||||
});
|
||||
auth.login({
|
||||
...tokenRes.data,
|
||||
});
|
||||
|
||||
onLoggedIn(loginWithPassResult.userId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user