-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created profile page in the admin side using ngrx
- Loading branch information
1 parent
2d95874
commit c30311d
Showing
14 changed files
with
175 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
export default authMiddleware = (req, res, next) => { | ||
const token = res.header("Authorization"); | ||
if (!token) return res.status(401).json({ message: "You are not authorized" }); | ||
|
||
const authMiddleware = (req, res, next) => { | ||
const authHeader = req.headers.authorization; | ||
if (!authHeader || !authHeader.startsWith("Bearer ")) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
const token = authHeader.split(" ")[1]; | ||
try { | ||
const decoded = jwt.verify(token, process.env.SECRET_KEY); | ||
req.user = decoded; | ||
|
||
next(); | ||
} catch (error) { | ||
res.status(401).json({ message: "Invalid Token" }); | ||
return res.status(401).json({ message: "Invalid token" }); | ||
} | ||
}; | ||
|
||
export default authMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import express from "express"; | ||
const router = express.Router(); | ||
import User from "../Models/UserModels.js"; | ||
import authMiddleware from "../Middleware/authMiddleware.js"; | ||
|
||
export default router.get("/profile", authMiddleware, async (req, res) => { | ||
const router = express.Router(); | ||
|
||
router.get("/profile", authMiddleware, async (req, res) => { | ||
try { | ||
const user = await User.findById(req.user._id); | ||
const user = await User.findById(req.user.id).select("-password"); | ||
if (!user) { | ||
res.status(404).json({ message: "User not found" }); | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
res.json(user); | ||
} catch (error) { | ||
console.error("Profile Error:", error); | ||
res.status(500).json({ message: "Server error" }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="container-fluid"> | ||
<div *ngIf="loading$ | async; else content"> | ||
<p>Loading profile data...</p> | ||
</div> | ||
|
||
<ng-template #content> | ||
<!-- <pre>{{ admin$ | async | json }}</pre> --> | ||
|
||
<div *ngIf="admin$ | async as admin; else error"> | ||
<h2>Admin Profile</h2> | ||
<p><strong>Name:</strong> {{ admin.first_name }} {{ admin.last_name }}</p> | ||
<p><strong>Email:</strong> {{ admin.username }}</p> | ||
</div> | ||
|
||
<ng-template #error> | ||
<div *ngIf="error$ | async as error"> | ||
<p>Error loading profile: {{ error }}</p> | ||
</div> | ||
</ng-template> | ||
</ng-template> | ||
</div> |
30 changes: 26 additions & 4 deletions
30
faculty-management/src/app/admin/components/admin-profile/admin-profile.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { Observable } from 'rxjs'; | ||
import { | ||
selectProfile, | ||
selectProfileLoading, | ||
selectProfileError, | ||
} from './store/profile.selectors'; | ||
import { ProfileActions } from './store/profile.actions'; | ||
import { CommonModule } from '@angular/common'; | ||
|
||
@Component({ | ||
selector: 'app-admin-profile', | ||
imports: [], | ||
imports: [CommonModule], | ||
templateUrl: './admin-profile.component.html', | ||
styleUrl: './admin-profile.component.css' | ||
styleUrls: ['./admin-profile.component.css'], | ||
}) | ||
export class AdminProfileComponent { | ||
export class AdminProfileComponent implements OnInit { | ||
admin$: Observable<any>; | ||
loading$: Observable<boolean>; | ||
error$: Observable<string | null>; | ||
|
||
constructor(private store: Store) { | ||
this.admin$ = this.store.select(selectProfile); | ||
this.loading$ = this.store.select(selectProfileLoading); | ||
this.error$ = this.store.select(selectProfileError); | ||
} | ||
|
||
ngOnInit(): void { | ||
this.store.dispatch(ProfileActions.loadProfiles()); | ||
console.log('this is the frontend' + this.admin$); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 17 additions & 18 deletions
35
faculty-management/src/app/admin/components/admin-profile/store/profile.effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Actions, createEffect, ofType } from '@ngrx/effects'; | ||
import { catchError, map, concatMap } from 'rxjs/operators'; | ||
import { Observable, EMPTY, of } from 'rxjs'; | ||
import { ProfileActions } from './profile.actions'; | ||
|
||
import { catchError, map, mergeMap, of } from 'rxjs'; | ||
import * as ProfileActions from '../store/profile.actions'; | ||
import { Action } from '@ngrx/store'; | ||
import { AdminAuthService } from '../../../service/admin-auth.service'; | ||
|
||
@Injectable() | ||
export class ProfileEffects { | ||
constructor(private actions$: Actions, private adminService: AdminAuthService) {} | ||
|
||
loadProfiles$ = createEffect(() => { | ||
return this.actions$.pipe( | ||
|
||
ofType(ProfileActions.loadProfiles), | ||
concatMap(() => | ||
/** An EMPTY observable only emits completion. Replace with your own observable API request */ | ||
EMPTY.pipe( | ||
map(data => ProfileActions.loadProfilesSuccess({ data })), | ||
catchError(error => of(ProfileActions.loadProfilesFailure({ error })))) | ||
loadProfile$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(ProfileActions.ProfileActions.loadProfiles), | ||
mergeMap(() => | ||
this.adminService.getProfile().pipe( | ||
map((profile): Action => ProfileActions.ProfileActions.loadProfilesSuccess({ profile })), // Ensure explicit Action type | ||
catchError((error) => | ||
of(ProfileActions.ProfileActions.loadProfilesFailure({ error: error.message })) // Wrap in `of()` to return an observable | ||
) | ||
) | ||
) | ||
); | ||
}); | ||
|
||
|
||
constructor(private actions$: Actions) {} | ||
) | ||
); | ||
} |
42 changes: 25 additions & 17 deletions
42
faculty-management/src/app/admin/components/admin-profile/store/profile.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { createFeature, createReducer, on } from '@ngrx/store'; | ||
import { createReducer, on } from '@ngrx/store'; | ||
import { ProfileActions } from './profile.actions'; | ||
|
||
export const profileFeatureKey = 'profile'; | ||
|
||
export interface State { | ||
|
||
export interface ProfileState { | ||
profile: any | null; | ||
loading: boolean; | ||
error: string | null; | ||
} | ||
|
||
export const initialState: State = { | ||
|
||
const initialState: ProfileState = { | ||
profile: null, | ||
loading: false, | ||
error: null, | ||
}; | ||
|
||
export const reducer = createReducer( | ||
export const profileReducer = createReducer( | ||
initialState, | ||
on(ProfileActions.loadProfiles, state => state), | ||
on(ProfileActions.loadProfilesSuccess, (state, action) => state), | ||
on(ProfileActions.loadProfilesFailure, (state, action) => state), | ||
on(ProfileActions.loadProfiles, (state) => ({ | ||
...state, | ||
loading: true, | ||
error: null, | ||
})), | ||
on(ProfileActions.loadProfilesSuccess, (state, { profile }) => ({ | ||
...state, | ||
profile, | ||
loading: false, | ||
})), | ||
on(ProfileActions.loadProfilesFailure, (state, { error }) => ({ | ||
...state, | ||
loading: false, | ||
error, | ||
})) | ||
); | ||
|
||
export const profileFeature = createFeature({ | ||
name: profileFeatureKey, | ||
reducer, | ||
}); | ||
|
21 changes: 19 additions & 2 deletions
21
faculty-management/src/app/admin/components/admin-profile/store/profile.selectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
import { createFeatureSelector, createSelector } from '@ngrx/store'; | ||
import * as fromProfile from './profile.reducer'; | ||
|
||
export const selectProfileState = createFeatureSelector<fromProfile.State>( | ||
fromProfile.profileFeatureKey | ||
export const selectProfileState = | ||
createFeatureSelector<fromProfile.ProfileState>('profile'); | ||
|
||
// selector to get the profile data | ||
export const selectProfile = createSelector( | ||
selectProfileState, | ||
(state) => state.profile | ||
); | ||
|
||
// selector to get the loading state | ||
export const selectProfileLoading = createSelector( | ||
selectProfileState, | ||
(state) => state.loading | ||
); | ||
|
||
// selector to get the error state | ||
export const selectProfileError = createSelector( | ||
selectProfileState, | ||
(state) => state.error | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.