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

Manages authentication state and provides context to the app.

// src/context/AuthContext.tsx

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

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

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

export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [isAdmin, setIsAdmin] = useState<boolean>(() => {
    const storedIsAdmin = localStorage.getItem('isAdmin');
    return storedIsAdmin === 'true';
  });

  const loginAsAdmin = async (username: string, password: string) => {
    // Simulate authentication
    if (username === 'admin' && password === 'password') {
      setIsAdmin(true);
      localStorage.setItem('isAdmin', 'true');
    } else {
      throw new Error('Invalid credentials');
    }
  };

  const logout = () => {
    setIsAdmin(false);
    localStorage.removeItem('isAdmin');
  };

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

export const useAuth = () => {
  const context = useContext(AuthContext);
  if (context === undefined) {
    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:
    • Stores login state in localStorage to persist across sessions.
  • Context API:
    • Provides authentication functions (loginAsAdmin, logout) to the rest of the app.

@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...

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: Backlog
Development

No branches or pull requests

2 participants
@mcochranca and others