mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-04 13:40:43 -03:00
primera iteracion de login + register con google funcionales
This commit is contained in:
30
src/lib/hooks/loginFirebase.ts
Normal file
30
src/lib/hooks/loginFirebase.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { apiBase } from '@/stores/url';
|
||||
import type { LoginSsoDto, Sesion } from '../../types';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { goto } from '$app/navigation';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
export async function loginFirebase(dto: LoginSsoDto, callbackfn: () => void) {
|
||||
if (dto.accessToken == '' || dto.uid == '') return;
|
||||
try {
|
||||
const req = await fetch(get(apiBase) + '/api/auth/login/sso', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
if (req.ok) {
|
||||
const token: Sesion = await req.json();
|
||||
console.log(token);
|
||||
sesionStore.set(token);
|
||||
goto('/');
|
||||
} else {
|
||||
callbackfn();
|
||||
}
|
||||
} catch {
|
||||
// callbackfn();
|
||||
console.error('fallo al intentar alcanzar el servidor');
|
||||
}
|
||||
}
|
||||
30
src/lib/hooks/registerFirebase.ts
Normal file
30
src/lib/hooks/registerFirebase.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { apiBase } from '@/stores/url';
|
||||
import { goto } from '$app/navigation';
|
||||
import type { RegisterDto, RegisterSsoDto } from '../../types';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
export async function registerFirebase(
|
||||
dto: RegisterSsoDto,
|
||||
callbackfn: () => void,
|
||||
admin: boolean = false
|
||||
) {
|
||||
if (dto.uid == '' || dto.token == '' || !dto.email?.includes('@') || dto.username == '') return;
|
||||
try {
|
||||
const req = await fetch(get(apiBase) + '/api/auth/register/sso', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
if (req.ok) {
|
||||
const data = await req.json();
|
||||
if (!admin) goto('/login?msg=' + data.message);
|
||||
} else {
|
||||
callbackfn();
|
||||
}
|
||||
} catch {
|
||||
callbackfn();
|
||||
console.error('fallo al intentar alcanzar el servidor');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user