SolidJSS
SolidJSโ€ข3y agoโ€ข
4 replies
Nin

Navigate user in login/logout functions

I'm trying to make it generic to send my users either to the main page or to the login page when they either login or logout but I'm having trouble doing this in a generic way.

import { createContext, createSignal, useContext } from "solid-js"
import type { Session } from "@supabase/supabase-js"

import supabase from "../supabase"

export const AuthContext = createContext<{
  session: () => Session | null
  login: (session: Session | null) => void
  logout: () => void
}>()

const [session, setSession] = createSignal<Session | null>(null)

export const authContext = {
  session: () => session(),
  login: (supabaseSession: Session | null) => {
    setSession(supabaseSession)
  },
  logout: () => {
    setSession(null)
    supabase.auth.signOut()
  }
}
export const useAuth = () => useContext(AuthContext)!


This is my Auth Provider for example, I would like to add to the login/logout sessions a redirect.

I've tried to use useNavigate but get the error it should be wrapped in a <Router />, I've tried using return <Navigate href="/" /> but this doesn't work either. Anyone able to help me out here?
Was this page helpful?