Guest User

Untitled

a guest
May 26th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use client'
  2.  
  3. import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
  4. import { useRouter } from 'next/navigation'
  5. import { useState } from 'react'
  6.  
  7. import type { Database } from '../../lib/supabase.types'
  8. export default function Login() {
  9.   const [email, setEmail] = useState('')
  10.   const [password, setPassword] = useState('')
  11.   const router = useRouter()
  12.   const supabase = createClientComponentClient<Database>()
  13.  
  14.   const handleSignUp = async () => {
  15.     await supabase.auth.signUp({
  16.       email,
  17.       password,
  18.       options: {
  19.         emailRedirectTo: `${location.origin}/auth/callback`,
  20.       },
  21.     })
  22.     router.refresh()
  23.   }
  24.  
  25.   const handleSignIn = async () => {
  26.     await supabase.auth.signInWithPassword({
  27.       email,
  28.       password,
  29.     })
  30.     router.refresh()
  31.   }
  32.  
  33.   const handleSignOut = async () => {
  34.     await supabase.auth.signOut()
  35.     router.refresh()
  36.   }
  37.   const getUser = async () => {
  38.     const { data, error } = await supabase.auth.getSession()
  39.     console.log("Data is", data) -> returns session: null
  40.     return data
  41.   }
  42.   console.log("supabase user is", getUser()) <- returns promis <state>: "pending"
  43.   return (
  44.     <>
  45.       <input name="email" onChange={(e) => setEmail(e.target.value)} value={email} />
  46.       <input
  47.         type="password"
  48.         name="password"
  49.         onChange={(e) => setPassword(e.target.value)}
  50.         value={password}
  51.       />
  52.       <button onClick={handleSignUp}>Sign up</button>
  53.       <button onClick={handleSignIn}>Sign in</button>
  54.       <button onClick={handleSignOut}>Sign out</button>
  55.     </>
  56.   )
  57. }
Advertisement
Add Comment
Please, Sign In to add comment