fetch auth session data from token
This commit is contained in:
42
frontend/lib/api/auth/getSession.ts
Normal file
42
frontend/lib/api/auth/getSession.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { AuthSessionType, AuthUser } from '~/types/auth';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
|
||||
export async function getAuthSessionData(params: {
|
||||
sessionType: AuthSessionType;
|
||||
sessionId: string;
|
||||
}): Promise<
|
||||
{ success: true; user: AuthUser; expiresAt: number } | { success: false }
|
||||
> {
|
||||
const { data, status } = await useFetch<
|
||||
ApiResponse<{
|
||||
user: AuthUser;
|
||||
expiresAt: number;
|
||||
}>
|
||||
>(getApiUrl('auth/session'), {
|
||||
method: 'GET',
|
||||
key: new Date().getTime().toString(),
|
||||
headers: {
|
||||
authorization: `${params.sessionType} ${params.sessionId}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (status.value !== 'success' || data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { id, name, email, admin } = data.value.data.user;
|
||||
const expiresAt = data.value.data.expiresAt;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
admin,
|
||||
},
|
||||
expiresAt,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user