Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context/AuthContext.tsx #1

Open
mcochranca opened this issue Oct 10, 2024 · 2 comments
Open

context/AuthContext.tsx #1

mcochranca opened this issue Oct 10, 2024 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed

Comments

@mcochranca
Copy link
Owner

mcochranca commented Oct 10, 2024

// src/context/AuthContext.tsx

import React, { createContext, useState, useContext, useEffect } from 'react';

interface AuthContextType {
isAdmin: boolean;
loginAsAdmin: (username: string, password: string) => Promise;
logout: () => void;
}

const AuthContext = createContext<AuthContextType | undefined>(undefined);

export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [isAdmin, setIsAdmin] = useState(false);

// Load authentication state from localStorage on mount
useEffect(() => {
const storedIsAdmin = localStorage.getItem('isAdmin');
if (storedIsAdmin === 'true') {
setIsAdmin(true);
}
}, []);

// Function to handle admin login
const loginAsAdmin = async (username: string, password: string): Promise => {
return new Promise((resolve, reject) => {
// Simulate authentication
if (username === 'admin' && password === 'password') {
setIsAdmin(true);
localStorage.setItem('isAdmin', 'true');
resolve();
} else {
reject(new Error('Invalid credentials'));
}
});
};

// Function to handle logout
const logout = (): void => {
setIsAdmin(false);
localStorage.removeItem('isAdmin');
};

return (
<AuthContext.Provider value={{ isAdmin, loginAsAdmin, logout }}>
{children}
</AuthContext.Provider>
);
};

export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};

/**

  • Notes:
    • Authentication State:
    • Manages isAdmin state to control access to admin routes.
    • Persistent Login:
    • Synchronizes login state with localStorage to persist across sessions.
    • Ensures isAdmin is initialized on app load.
    • Context API:
    • Provides loginAsAdmin and logout functions for authentication management.
    • Error Handling:
    • Throws an error if useAuth is used outside of AuthProvider.
      */
@mcochranca mcochranca self-assigned this Oct 10, 2024
@mcochranca mcochranca converted this from a draft issue Oct 10, 2024
Copy link

Message that will be displayed on users' first issue

@mcochranca mcochranca added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed labels Oct 10, 2024
@mcochranca
Copy link
Owner Author

yup...

@mcochranca mcochranca moved this from Backlog to Ready in OpenHAR Dec 20, 2024
@mcochranca mcochranca pinned this issue Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed
Projects
Status: Ready
Development

No branches or pull requests

2 participants
@mcochranca and others