created User-Model and Employee-Model
This commit is contained in:
25
app/Controllers/Http/EmployeesController.ts
Normal file
25
app/Controllers/Http/EmployeesController.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class EmployeesController {
|
||||
public async index ({}: HttpContextContract) {
|
||||
|
||||
}
|
||||
|
||||
public async create ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async store ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async show ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async edit ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async update ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async destroy ({}: HttpContextContract) {
|
||||
}
|
||||
}
|
||||
76
app/Middleware/Auth.ts
Normal file
76
app/Middleware/Auth.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { GuardsList } from '@ioc:Adonis/Addons/Auth'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import { AuthenticationException } from '@adonisjs/auth/build/standalone'
|
||||
|
||||
/**
|
||||
* Auth middleware is meant to restrict un-authenticated access to a given route
|
||||
* or a group of routes.
|
||||
*
|
||||
* You must register this middleware inside `start/kernel.ts` file under the list
|
||||
* of named middleware.
|
||||
*/
|
||||
export default class AuthMiddleware {
|
||||
/**
|
||||
* The URL to redirect to when request is Unauthorized
|
||||
*/
|
||||
protected redirectTo = '/login'
|
||||
|
||||
/**
|
||||
* Authenticates the current HTTP request against a custom set of defined
|
||||
* guards.
|
||||
*
|
||||
* The authentication loop stops as soon as the user is authenticated using any
|
||||
* of the mentioned guards and that guard will be used by the rest of the code
|
||||
* during the current request.
|
||||
*/
|
||||
protected async authenticate(auth: HttpContextContract['auth'], guards: (keyof GuardsList)[]) {
|
||||
/**
|
||||
* Hold reference to the guard last attempted within the for loop. We pass
|
||||
* the reference of the guard to the "AuthenticationException", so that
|
||||
* it can decide the correct response behavior based upon the guard
|
||||
* driver
|
||||
*/
|
||||
let guardLastAttempted: string | undefined
|
||||
|
||||
for (let guard of guards) {
|
||||
guardLastAttempted = guard
|
||||
|
||||
if (await auth.use(guard).check()) {
|
||||
/**
|
||||
* Instruct auth to use the given guard as the default guard for
|
||||
* the rest of the request, since the user authenticated
|
||||
* succeeded here
|
||||
*/
|
||||
auth.defaultGuard = guard
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unable to authenticate using any guard
|
||||
*/
|
||||
throw new AuthenticationException(
|
||||
'Unauthorized access',
|
||||
'E_UNAUTHORIZED_ACCESS',
|
||||
guardLastAttempted,
|
||||
this.redirectTo,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request
|
||||
*/
|
||||
public async handle (
|
||||
{ auth }: HttpContextContract,
|
||||
next: () => Promise<void>,
|
||||
customGuards: (keyof GuardsList)[]
|
||||
) {
|
||||
/**
|
||||
* Uses the user defined guards or the default guard mentioned in
|
||||
* the config file
|
||||
*/
|
||||
const guards = customGuards.length ? customGuards : [auth.name]
|
||||
await this.authenticate(auth, guards)
|
||||
await next()
|
||||
}
|
||||
}
|
||||
21
app/Middleware/SilentAuth.ts
Normal file
21
app/Middleware/SilentAuth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
/**
|
||||
* Silent auth middleware can be used as a global middleware to silent check
|
||||
* if the user is logged-in or not.
|
||||
*
|
||||
* The request continues as usual, even when the user is not logged-in.
|
||||
*/
|
||||
export default class SilentAuthMiddleware {
|
||||
/**
|
||||
* Handle request
|
||||
*/
|
||||
public async handle({ auth }: HttpContextContract, next: () => Promise<void>) {
|
||||
/**
|
||||
* Check if user is logged-in or not. If yes, then `ctx.auth.user` will be
|
||||
* set to the instance of the currently logged in user.
|
||||
*/
|
||||
await auth.check()
|
||||
await next()
|
||||
}
|
||||
}
|
||||
41
app/Models/Employee.ts
Normal file
41
app/Models/Employee.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, belongsTo, BelongsTo, column } from '@ioc:Adonis/Lucid/Orm'
|
||||
import User from 'App/Models/User'
|
||||
|
||||
export default class Employee extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime
|
||||
|
||||
@column()
|
||||
public firstName : string
|
||||
|
||||
@column()
|
||||
public lastName : string
|
||||
|
||||
@column()
|
||||
public shorthand: string
|
||||
|
||||
@column()
|
||||
public email : string
|
||||
|
||||
@column()
|
||||
public phone : string
|
||||
|
||||
@column()
|
||||
public mobile : string
|
||||
|
||||
@column()
|
||||
public contractHours : number
|
||||
|
||||
@column()
|
||||
public userId : number
|
||||
|
||||
@belongsTo(() => User)
|
||||
public user: BelongsTo<typeof User>
|
||||
}
|
||||
35
app/Models/User.ts
Normal file
35
app/Models/User.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, beforeSave, column, hasOne, HasOne } from '@ioc:Adonis/Lucid/Orm'
|
||||
import Hash from '@ioc:Adonis/Core/Hash'
|
||||
import Employee from 'App/Models/Employee'
|
||||
|
||||
export default class User extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime
|
||||
|
||||
@column()
|
||||
public username: string
|
||||
|
||||
@column()
|
||||
public email: string
|
||||
|
||||
@column({serializeAs: null})
|
||||
public password : string
|
||||
|
||||
@hasOne(() => Employee)
|
||||
public employeeProfile : HasOne<typeof Employee>
|
||||
|
||||
@beforeSave()
|
||||
public static async hashPassword(user: User) {
|
||||
if(user.$dirty.password){
|
||||
user.password = await Hash.make(user.password)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user