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

Add authentication context #54

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@
"evenBetterToml.formatter.reorderArrays": true,
"evenBetterToml.formatter.trailingNewline": true,
// We like keeping TOML keys in a certain non-alphabetical order that feels more natural
"evenBetterToml.formatter.reorderKeys": false
"evenBetterToml.formatter.reorderKeys": false,
"[typescriptreact]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
}
}
1 change: 1 addition & 0 deletions canopeum_frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ module.exports = {
'no-autofix/no-relative-import-paths/no-relative-import-paths': 'off',
// Using Bootraps directly without a React wrapper will cause us to have to add classes to React Components
'react/forbid-component-props': 'off',
"react/display-name": [false, { "ignoreTranspilerName": false, "checkContextObjects": false }]
},
}
30 changes: 17 additions & 13 deletions canopeum_frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ import Map from './pages/Map';
import MapSite from './pages/MapSite';
import UserManagement from './pages/UserManagement';
import Utilities from './pages/Utilities';
import AuthenticationContextProvider from './components/context/AuthenticationContext';

const App = () => (
<BrowserRouter>
<Navbar />
<Routes>
<Route element={<Home />} path="/home" />
<Route element={<Home />} path="/" />
<Route element={<Analytics />} path="/analytics" />
<Route element={<Map />} path="/map" />
<Route element={<MapSite />} path='/map/:siteId'/>
<Route element={<UserManagement />} path="/user-management" />
<Route element={<Login />} path="/login" />
<Route element={<Utilities />} path="/utilities" />
</Routes>
</BrowserRouter>
<AuthenticationContextProvider>
<BrowserRouter>
<Navbar />
<Routes>
<Route element={<Home />} path="/home" />
<Route element={<Home />} path="/" />
<Route element={<Analytics />} path="/analytics" />
<Route element={<Map />} path="/map" />
<Route element={<MapSite />} path='/map/:siteId' />
<Route element={<UserManagement />} path="/user-management" />
<Route element={<Login />} path="/login" />
<Route element={<Utilities />} path="/utilities" />
</Routes>
</BrowserRouter>
</AuthenticationContextProvider>

);

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { FunctionComponent, ReactNode } from 'react';
import { createContext, memo, useState } from 'react'

export enum UserRole {
MegaAdmin = 'MegaAdmin',
MiniAdmin = 'MiniAdmin',
RegularUser = 'RegularUser',
}

type User = {
firstname: string
lastname: string,
email: string,
role: UserRole,
image: string
}

type IAuthenticationContext = {
authenticate: (user: User) => void
isAuthenticated: boolean
currentUser: User | undefined
}

export const AuthenticationContext = createContext<IAuthenticationContext>({
authenticate: (_: User) => { },
isAuthenticated: false,
currentUser: undefined
});

const AuthenticationContextProvider: FunctionComponent<{ readonly children?: ReactNode }> = memo((props) => {
const [user, setUser] = useState<User | undefined>(undefined)

const authenticate = (user: User) => setUser(user)

return <AuthenticationContext.Provider
value={{
currentUser: user,
isAuthenticated: user !== undefined,
authenticate,
}}>
{props.children}
</AuthenticationContext.Provider>
})

export default AuthenticationContextProvider
34 changes: 17 additions & 17 deletions canopeum_frontend/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ const Home = () => {
<div className="bg-white rounded-2 px-3 py-2">
<h1>Home</h1>
{isLoading
? (
<p>Loading...</p>
)
: error
? (
<p>Error: {error.message}</p>
)
: (
<div>
<p>Example request from API:</p>
<ul>
{data.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
)}
? (
<p>Loading...</p>
)
: error
? (
<p>Error: {error.message}</p>
)
: (
<div>
<p>Example request from API:</p>
<ul>
{data.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
)}
</div>
</div>
</div>
Expand Down
Loading