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
Pod14mo 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
sarahmiller32114mo ago
what do you mean? thats the point no? they won't be logged in
Pod
Pod14mo ago
This is an example of how it would be used
sarahmiller321
sarahmiller32114mo ago
whats the difference here? with serverSideProps @Pod
Pod
Pod14mo ago
It will only run once, and it will get the auth session if it exists
sarahmiller321
sarahmiller32114mo ago
where does getServerSession come from Next/auth?
Pod
Pod14mo ago
It's straight nextjs
Ramsay
Ramsay14mo 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.