Use Accept header to avoid unnecessary call to get current user
This commit is contained in:
parent
868f950559
commit
0ef310fecd
@ -3,17 +3,25 @@
|
||||
import { getCsrfToken } from '@/app/lib/utils';
|
||||
import { User } from '@/app/lib/definitions';
|
||||
|
||||
export function login({ email, password, onLogin }: { email: string, password: string, onLogin: () => void }) {
|
||||
export function login({ email, password, onLogin }: { email: string, password: string, onLogin: (user: User) => void }) {
|
||||
console.log(email, password);
|
||||
return fetch("/api/default/users/sign_in", {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ user: { email, password } }),
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-TOKEN': getCsrfToken(),
|
||||
}
|
||||
})
|
||||
.then(onLogin)
|
||||
.then((response) => response.json())
|
||||
.then((data: any) => {
|
||||
console.log(data);
|
||||
onLogin({
|
||||
id: data.id || '',
|
||||
email: data.email || '',
|
||||
});
|
||||
})
|
||||
.catch((error) => console.error(error));
|
||||
}
|
||||
|
||||
@ -26,13 +34,3 @@ export function logout({ onLogout }: { onLogout: () => void }) {
|
||||
}).then(onLogout)
|
||||
.catch((error) => console.error(error));
|
||||
}
|
||||
|
||||
export function getCurrentUser({ onLoad }: { onLoad: (user: User) => void }) {
|
||||
fetch("/api/default/users/current")
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
onLoad(data);
|
||||
}, (error) => {
|
||||
return null;
|
||||
});
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
|
||||
import { FloatLabel } from 'primereact/floatlabel';
|
||||
import { InputText } from 'primereact/inputtext';
|
||||
import { getCurrentUser, login } from '../../api/authentication';
|
||||
import { login } from '../../api/authentication';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { classNames } from './button';
|
||||
import { useRouter } from 'next/navigation'
|
||||
@ -38,8 +38,8 @@ export default function LoginForm() {
|
||||
onClick={() => login({
|
||||
email: email,
|
||||
password: password,
|
||||
onLogin: () => {
|
||||
getCurrentUser({ onLoad: (user) => setCurrentUser(user) });
|
||||
onLogin: (user) => {
|
||||
setCurrentUser(user);
|
||||
router.push('/dashboard')
|
||||
}
|
||||
})}>
|
||||
|
Loading…
x
Reference in New Issue
Block a user