mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-07-09 15:08:06 +00:00
Fix Register & auto connect when register
Took 12 minutes
This commit is contained in:
@@ -28,9 +28,9 @@ export default function LoginForm() {
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
await signIn('credentials',
|
await signIn('credentials',
|
||||||
{...loginData, redirect: false}).then((res) => {
|
{...loginData, redirect: false}).then((res) => {
|
||||||
const {ok} = res as SignInResponse;
|
const {ok: connexionSuccess} = res as SignInResponse;
|
||||||
|
|
||||||
if (!ok) setInvalidInput(true);
|
if (!connexionSuccess) setInvalidInput(true);
|
||||||
else router.push('/');
|
else router.push('/');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import {useRouter} from 'next/router';
|
import {useRouter} from 'next/router';
|
||||||
import {useState} from 'react';
|
import {useState} from 'react';
|
||||||
import {RegisterData} from '@/models/form/RegisterData';
|
import {RegisterData} from '@/models/form/RegisterData';
|
||||||
|
import {signIn, SignInResponse} from 'next-auth/react';
|
||||||
|
|
||||||
export default function RegisterForm() {
|
export default function RegisterForm() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -27,11 +28,22 @@ export default function RegisterForm() {
|
|||||||
const {password, confirmPassword} = registerData;
|
const {password, confirmPassword} = registerData;
|
||||||
if (password !== confirmPassword) setInvalidInput(true);
|
if (password !== confirmPassword) setInvalidInput(true);
|
||||||
|
|
||||||
fetch('/api/user/new', {
|
fetch('/api/user', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify(registerData),
|
body: JSON.stringify(registerData),
|
||||||
}).then(() => router.push('/')).catch(() => {
|
}).then(() => {
|
||||||
|
const {email, password} = registerData;
|
||||||
|
|
||||||
|
signIn('credentials',
|
||||||
|
{email, password, redirect: false}).then((res) => {
|
||||||
|
const {ok: connexionSuccess} = res as SignInResponse;
|
||||||
|
|
||||||
|
// TODO If success -> goto interactive form else login
|
||||||
|
router.push( connexionSuccess ? "/" : "/login");
|
||||||
|
});
|
||||||
|
|
||||||
|
}).catch(() => {
|
||||||
setInvalidInput(true);
|
setInvalidInput(true);
|
||||||
setRegisterData({...registerData, password: '', confirmPassword: ''});
|
setRegisterData({...registerData, password: '', confirmPassword: ''});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user