This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds auth data from other collections during user login
- Loading branch information
Showing
8 changed files
with
126 additions
and
25 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,22 +1,100 @@ | ||
import { ServiceAddons } from '@feathersjs/feathers'; | ||
import { HookContext, Paginated, Params, ServiceAddons } from '@feathersjs/feathers'; | ||
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication'; | ||
import { LocalStrategy } from '@feathersjs/authentication-local'; | ||
import { expressOauth } from '@feathersjs/authentication-oauth'; | ||
|
||
import { Application } from './declarations'; | ||
import { Users } from './services/users/users.class'; | ||
import RolesEnum from './constants/roles.enum'; | ||
import { Player } from './services/player/player.class'; | ||
import { PlatformSuperAdmin } from './services/platform-super-admin/platform-super-admin.class'; | ||
import { Admin } from './services/admin/admin.class'; | ||
|
||
declare module './declarations' { | ||
interface ServiceTypes { | ||
'authentication': AuthenticationService & ServiceAddons<any>; | ||
} | ||
} | ||
|
||
export default function(app: Application): void { | ||
export default function (app: Application): void { | ||
const authentication = new AuthenticationService(app); | ||
|
||
class CustomLocalStrategy extends LocalStrategy { | ||
async findEntity(username: string, params: Params): Promise<any> { | ||
// Find the user entity | ||
console.log(`username = ${username}`) | ||
const UserService: Users & ServiceAddons<any> = app.service('users'); | ||
const user: any[] | Paginated<any> = await UserService.find({ | ||
query: { email: username }, | ||
paginate: false // To get only one user | ||
}); | ||
// @ts-ignore | ||
if (user.length === 0) { | ||
return null; // User not found | ||
} | ||
|
||
// Get the first user (assuming email is unique) | ||
// @ts-ignore | ||
const foundUser = user[0]; | ||
return { ...foundUser }; | ||
} | ||
} | ||
|
||
authentication.register('jwt', new JWTStrategy()); | ||
authentication.register('local', new LocalStrategy()); | ||
authentication.register('local', new CustomLocalStrategy()); | ||
|
||
app.use('/authentication', authentication); | ||
app.configure(expressOauth()); | ||
const service = app.service('authentication'); | ||
|
||
service.hooks({ | ||
after: { | ||
create: [ | ||
async (context: HookContext) => { | ||
const { user } = context.result; | ||
console.log(user); | ||
|
||
if (user.role === RolesEnum.PLAYER) { | ||
// Load player data | ||
let playerData = null; | ||
const playerService: Player & ServiceAddons<any> = app.service('player'); | ||
const player = await playerService._find({ | ||
query: { user: user._id }, | ||
paginate: false | ||
}); | ||
playerData = player.length > 0 ? player[0] : null; | ||
|
||
console.log(playerData) | ||
context.result.user.playerData = playerData; | ||
} | ||
|
||
if (user.role === RolesEnum.ADMIN) { | ||
let adminData = null; | ||
const adminService: Admin & ServiceAddons<any> = app.service('admin'); | ||
const psa = await adminService._find({ | ||
query: { user: user._id }, | ||
paginate: false | ||
}); | ||
|
||
adminData = psa.length > 0 ? psa[0] : null; | ||
console.log(adminData) | ||
context.result.user.adminData = adminData; | ||
} | ||
|
||
if (user.role === RolesEnum.PLATFORM_SUPER_ADMIN) { | ||
let psaData = null; | ||
const psaService: PlatformSuperAdmin & ServiceAddons<any> = app.service('platform-super-admin'); | ||
const psa = await psaService._find({ | ||
query: { user: user._id }, | ||
paginate: false | ||
}); | ||
psaData = psa.length > 0 ? psa[0] : null; | ||
|
||
console.log(psaData) | ||
context.result.user.psaData = psaData; | ||
} | ||
} | ||
] | ||
} | ||
}); | ||
} |
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 @@ | ||
// Use this hook to manipulate incoming or outgoing data. | ||
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html | ||
import { BadRequest } from '@feathersjs/errors'; | ||
import { Hook, HookContext } from '@feathersjs/feathers'; | ||
import RolesEnum from '../constants/roles.enum'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export default (options: object = {}): Hook => { | ||
return async (context: HookContext): Promise<HookContext> => { | ||
|
||
|
||
const targetId = context.id; | ||
const { user } = context.params; | ||
// @ts-ignore | ||
if(user.role === RolesEnum.PLATFORM_SUPER_ADMIN) return context | ||
// @ts-ignore | ||
const initiatorId = user._id.toString(); | ||
if(initiatorId === targetId) return context; | ||
throw new BadRequest('users cannot delete other users') | ||
}; | ||
}; |
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
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