Immediately prompting for signin

currently I have this in home:
const Home: NextPage = () => {
const { data: sessionData } = useSession();

React.useEffect(() => {
console.log(sessionData)
if(!sessionData) {
signIn()
} else {
signOut()
}
}, [])
const Home: NextPage = () => {
const { data: sessionData } = useSession();

React.useEffect(() => {
console.log(sessionData)
if(!sessionData) {
signIn()
} else {
signOut()
}
}, [])
but this is not working, I would like the user to sign in as soon as they hit the page and keep them logged in, however when they sign in, it asks them to sign again... always in a loop
8 Replies
Pod
Pod2y ago
@sarahmiller321 Pass the method you want them to sign in with in the function Also, the way you have it currently set up is a bit skewed. You'd probably want to check for auth in getServerSideProps, as with your useEffect, the very first pass won't have session data
sarahmiller321
what do you mean? thats the point no? they won't be logged in
Pod
Pod2y ago
This is an example of how it would be used
sarahmiller321
whats the difference here? with serverSideProps @Pod
Pod
Pod2y ago
It will only run once, and it will get the auth session if it exists
sarahmiller321
where does getServerSession come from Next/auth?
Pod
Pod2y ago
It's straight nextjs
Ramsay
Ramsay2y ago
If you want to redirect to sign in on the client rather than with getServerSideProps, you can just pass { required: true } to useSession https://next-auth.js.org/getting-started/client#require-session This will accomplish what you were trying to achieve with the useEffect.
Client API | NextAuth.js
The NextAuth.js client library makes it easy to interact with sessions from React applications.
Want results from more Discord servers?
Add your server