THE LAST ONE BEFORE THE RELEASE
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import React, { createContext, useContext, useState } from 'react'
|
||||
import type React from "react"
|
||||
import { createContext, useContext, useState, useEffect } from "react"
|
||||
|
||||
type AuthContextType = {
|
||||
isLoggedIn: boolean
|
||||
@@ -13,7 +14,7 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined)
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext)
|
||||
if (!context) {
|
||||
throw new Error('useAuth must be used within an AuthProvider')
|
||||
throw new Error("useAuth must be used within an AuthProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
@@ -21,13 +22,23 @@ export const useAuth = () => {
|
||||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [isLoggedIn, setIsLoggedIn] = useState(false)
|
||||
|
||||
const login = () => setIsLoggedIn(true)
|
||||
const logout = () => setIsLoggedIn(false)
|
||||
useEffect(() => {
|
||||
const storedLoginState = localStorage.getItem("isLoggedIn")
|
||||
if (storedLoginState === "true") {
|
||||
setIsLoggedIn(true)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isLoggedIn, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
const login = () => {
|
||||
setIsLoggedIn(true)
|
||||
localStorage.setItem("isLoggedIn", "true")
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
setIsLoggedIn(false)
|
||||
localStorage.removeItem("isLoggedIn")
|
||||
}
|
||||
|
||||
return <AuthContext.Provider value={{ isLoggedIn, login, logout }}>{children}</AuthContext.Provider>
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user