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 { getCsrfToken } from '@/app/lib/utils';
|
||||||
import { User } from '@/app/lib/definitions';
|
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);
|
console.log(email, password);
|
||||||
return fetch("/api/default/users/sign_in", {
|
return fetch("/api/default/users/sign_in", {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ user: { email, password } }),
|
body: JSON.stringify({ user: { email, password } }),
|
||||||
headers: {
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-CSRF-TOKEN': getCsrfToken(),
|
'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));
|
.catch((error) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,13 +34,3 @@ export function logout({ onLogout }: { onLogout: () => void }) {
|
|||||||
}).then(onLogout)
|
}).then(onLogout)
|
||||||
.catch((error) => console.error(error));
|
.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 { FloatLabel } from 'primereact/floatlabel';
|
||||||
import { InputText } from 'primereact/inputtext';
|
import { InputText } from 'primereact/inputtext';
|
||||||
import { getCurrentUser, login } from '../../api/authentication';
|
import { login } from '../../api/authentication';
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { classNames } from './button';
|
import { classNames } from './button';
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
@ -38,8 +38,8 @@ export default function LoginForm() {
|
|||||||
onClick={() => login({
|
onClick={() => login({
|
||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
onLogin: () => {
|
onLogin: (user) => {
|
||||||
getCurrentUser({ onLoad: (user) => setCurrentUser(user) });
|
setCurrentUser(user);
|
||||||
router.push('/dashboard')
|
router.push('/dashboard')
|
||||||
}
|
}
|
||||||
})}>
|
})}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user