From 803970889755c0c734a51063f01a0c25ccddf941 Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Thu, 1 Jul 2021 23:01:48 +0200 Subject: [PATCH] Working Signed-off-by: Arthur DANJOU --- app/Controllers/Http/AuthController.ts | 6 +-- app/Controllers/Http/StatesController.ts | 23 +++++---- config/auth.ts | 63 +++++++++++++++++++++++- contracts/auth.ts | 13 +++++ 4 files changed, 92 insertions(+), 13 deletions(-) diff --git a/app/Controllers/Http/AuthController.ts b/app/Controllers/Http/AuthController.ts index 57cc783..524b2ee 100755 --- a/app/Controllers/Http/AuthController.ts +++ b/app/Controllers/Http/AuthController.ts @@ -66,7 +66,7 @@ export default class AuthController { const twitterUser = await twitter.user() const user = await this.createUser(twitterUser) - await auth.login(user) + await auth.use('web').login(user) return response.status(200).send({ user: user }) @@ -95,7 +95,7 @@ export default class AuthController { const githubUser = await github.user() const user = await this.createUser(githubUser) - await auth.login(user) + await auth.use('web').login(user) return response.status(200).send({ user: user }) @@ -124,7 +124,7 @@ export default class AuthController { const googleUser = await google.user() const user = await this.createUser(googleUser) - await auth.login(user) + await auth.use('web').login(user) return response.status(200).send({ user: user }) diff --git a/app/Controllers/Http/StatesController.ts b/app/Controllers/Http/StatesController.ts index 765306f..9a1ebf3 100755 --- a/app/Controllers/Http/StatesController.ts +++ b/app/Controllers/Http/StatesController.ts @@ -2,19 +2,24 @@ import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext"; import Redis from "@ioc:Adonis/Addons/Redis"; import {UpdateGitHubReadme} from "App/Tasks/UpdateGithubReadme"; +const STATES = [ + {sleeping: 'is_sleeping'}, + {listening: 'is_listening_music'}, + {developing: 'is_developing'}, + {learning: 'is_learning'} +] + export default class StatesController { public async get({response}: HttpContextContract) { - const is_sleeping = await Redis.get('states:sleeping') - const is_listening_music = await Redis.get('states:listening') - const is_developing = await Redis.get('states:developing') - const is_learning = await Redis.get('states:learning') + const states = STATES.map(async state => { + return this.getStatus(await Redis.get(`states:${state}`)) + }) return response.status(200).send({ - is_learning: this.getStatus(is_learning), - is_sleeping: this.getStatus(is_sleeping), - is_developing: this.getStatus(is_developing), - is_listening_music: this.getStatus(is_listening_music) + states: { + ...states + } }) } @@ -48,7 +53,7 @@ export default class StatesController { await UpdateGitHubReadme() return response.status(200).send({ - message: 'State successfully updated !' + message: 'State successfully updated!' }) } } diff --git a/config/auth.ts b/config/auth.ts index 1209c01..c1b828d 100755 --- a/config/auth.ts +++ b/config/auth.ts @@ -5,7 +5,7 @@ * file. */ -import { AuthConfig } from '@ioc:Adonis/Addons/Auth' +import {AuthConfig} from '@ioc:Adonis/Addons/Auth' /* |-------------------------------------------------------------------------- @@ -19,6 +19,67 @@ import { AuthConfig } from '@ioc:Adonis/Addons/Auth' const authConfig: AuthConfig = { guard: 'api', guards: { + /* + |-------------------------------------------------------------------------- + | Web Guard + |-------------------------------------------------------------------------- + | + | Web guard uses classic old school sessions for authenticating users. + | If you are building a standard web application, it is recommended to + | use web guard with session driver + | + */ + web: { + driver: 'session', + + provider: { + /* + |-------------------------------------------------------------------------- + | Driver + |-------------------------------------------------------------------------- + | + | Name of the driver + | + */ + driver: 'lucid', + + /* + |-------------------------------------------------------------------------- + | Identifier key + |-------------------------------------------------------------------------- + | + | The identifier key is the unique key on the model. In most cases specifying + | the primary key is the right choice. + | + */ + identifierKey: 'id', + + /* + |-------------------------------------------------------------------------- + | Uids + |-------------------------------------------------------------------------- + | + | Uids are used to search a user against one of the mentioned columns. During + | login, the auth module will search the user mentioned value against one + | of the mentioned columns to find their user record. + | + */ + uids: ['email'], + + /* + |-------------------------------------------------------------------------- + | Model + |-------------------------------------------------------------------------- + | + | The model to use for fetching or finding users. The model is imported + | lazily since the config files are read way earlier in the lifecycle + | of booting the app and the models may not be in a usable state at + | that time. + | + */ + model: () => import('App/Models/User'), + }, + }, /* |-------------------------------------------------------------------------- | OAT Guard diff --git a/contracts/auth.ts b/contracts/auth.ts index 90916e2..a498628 100755 --- a/contracts/auth.ts +++ b/contracts/auth.ts @@ -55,6 +55,19 @@ declare module '@ioc:Adonis/Addons/Auth' { | */ interface GuardsList { + /* + |-------------------------------------------------------------------------- + | Web Guard + |-------------------------------------------------------------------------- + | + | The web guard uses sessions for maintaining user login state. It uses + | the `user` provider for fetching user details. + | + */ + web: { + implementation: SessionGuardContract<'user', 'web'>, + config: SessionGuardConfig<'user'>, + }, /* |-------------------------------------------------------------------------- | OAT Guard