Page navigation not working as desired
Answered
i_lost_to_loba_kreygasm posted this in #help-forum
So I have a google sign in button , I used router.push to navigate to home page but it returns to 'sign page' again . How to fix this?
Answered by Ray
"use client";
import { Container, TextInput, Button } from "@mantine/core";
import { signIn } from "next-auth/react";
import { useSession, getSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect } from 'react'
function Login() {
const { data, status } = useSession();
const router = useRouter();
useEffect(() => {
if (status === "authenticated") {
setTimeout(() => {
router.push("/")
}, 6000)
}
}, [status])
return (
<Container>
{status === "authenticated" ? (
<h1>You are logged in</h1>
) : (
<>
<TextInput w={320} mt={16} />
<TextInput w={320} mt={16} />
<Button
mt={16}
onClick={ () => {
signIn("google");
}}
>
Sign In
</Button>
</>
)}
</Container>
);
}
export default Login;34 Replies
<Button mt={16} onClick={async ()=>{
await signIn('google');
setTimeout(()=>{
router.push("/"),
12000
})
}}>Sign In</Button>This looks normal, are you sure nothing else routes you back?
yeah I am sure 😦
@i_lost_to_loba_kreygasm js
<Button mt={16} onClick={async ()=>{
await signIn('google');
setTimeout(()=>{
router.push("/"),
12000
})
}}>Sign In</Button>
setTimeout(()=>{
router.push("/")
},12000)@Ray ts
setTimeout(()=>{
router.push("/")
},12000)
thank you , i realized my mistake
but now it doesnt navigate at all 😦
<Button mt={16} onClick={async ()=>{
await signIn('google');
setTimeout(()=>{
router.push("/")
},6000)
}}>Sign In</Button>@i_lost_to_loba_kreygasm but now it doesnt navigate at all 😦
why you need setTimeout?
@i_lost_to_loba_kreygasm js
<Button mt={16} onClick={async ()=>{
await signIn('google');
setTimeout(()=>{
router.push("/")
},6000)
}}>Sign In</Button>
try this
<Button mt={16} onClick={async ()=>{
await signIn('google');
await new Promise(res => setTimeout(res,6000))
router.push("/")
}}>Sign In</Button>wait whats
res?a function ?
still doesnt navigate ,where am I doing wrong ?
could you show the full code
or do you see any error
@i_lost_to_loba_kreygasm are you importing
and using
useRouter();and using
const router = useRouter();?'use client';
import { Container,TextInput,Button } from "@mantine/core";
import {signIn} from 'next-auth/react';
import { useSession, getSession } from "next-auth/react";
import { useRouter } from 'next/navigation'
function Login(){
const { data, status } = useSession();
const router = useRouter();
return <Container>
{status==='authenticated'?<h1>You are logged in</h1>: <><TextInput w={320}mt={16}/>
<TextInput w={320} mt={16}/>
<Button mt={16} onClick={async ()=>{
await signIn('google');
await new Promise(res => setTimeout(res,6000))
router.push("/")
}}>Sign In</Button>
</>}
</Container>
}
export default Login;this is my whole code
dont see any
@i_lost_to_loba_kreygasm js
'use client';
import { Container,TextInput,Button } from "@mantine/core";
import {signIn} from 'next-auth/react';
import { useSession, getSession } from "next-auth/react";
import { useRouter } from 'next/navigation'
function Login(){
const { data, status } = useSession();
const router = useRouter();
return <Container>
{status==='authenticated'?<h1>You are logged in</h1>: <><TextInput w={320}mt={16}/>
<TextInput w={320} mt={16}/>
<Button mt={16} onClick={async ()=>{
await signIn('google');
await new Promise(res => setTimeout(res,6000))
router.push("/")
}}>Sign In</Button>
</>}
</Container>
}
export default Login;
It seems correct and I suggest try it with import { useRouter } from 'next/router';
I know use for page routing
I know use for page routing
@DEV_2.0 It seems correct and I suggest try it with import { useRouter } from 'next/router';
I know use for page routing
this doesn't work for app router
i used
next/router and it showed smth like router not mounted o.O@i_lost_to_loba_kreygasm dont see any
are you using next-auth v4?
"next-auth": "^4.24.7",looks like it
@i_lost_to_loba_kreygasm looks like it
"use client";
import { Container, TextInput, Button } from "@mantine/core";
import { signIn } from "next-auth/react";
import { useSession, getSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect } from 'react'
function Login() {
const { data, status } = useSession();
const router = useRouter();
useEffect(() => {
if (status === "authenticated") {
setTimeout(() => {
router.push("/")
}, 6000)
}
}, [status])
return (
<Container>
{status === "authenticated" ? (
<h1>You are logged in</h1>
) : (
<>
<TextInput w={320} mt={16} />
<TextInput w={320} mt={16} />
<Button
mt={16}
onClick={ () => {
signIn("google");
}}
>
Sign In
</Button>
</>
)}
</Container>
);
}
export default Login;Answer
try this
@i_lost_to_loba_kreygasm "next-auth": "^4.24.7",
const { data: session, status } = useSession()
try like this
try like this
@Ray try this
works 🙂
can you explain what you did ?
@i_lost_to_loba_kreygasm can you explain what you did ?
because
signIn is not a async function in v4