mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Working on auth
This commit is contained in:
@@ -23,7 +23,6 @@
|
||||
"providers": [
|
||||
"./providers/AppProvider",
|
||||
"@adonisjs/core",
|
||||
"@adonisjs/redis",
|
||||
"@adonisjs/session",
|
||||
"@adonisjs/auth",
|
||||
"@adonisjs/lucid",
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from "App/Models/User";
|
||||
import AuthValidator from "App/Validators/AuthValidator";
|
||||
|
||||
export default class AuthController {
|
||||
|
||||
public async loginApi ({ request, auth, response }: HttpContextContract) {
|
||||
const email = request.input('email')
|
||||
const password = request.input('password')
|
||||
const infinity = request.input('infinity', false)
|
||||
|
||||
const { email, password } = await request.validate(AuthValidator)
|
||||
const token = await auth.use('api').attempt(email, password, {
|
||||
expiresIn: infinity ? '' : '2 days'
|
||||
expiresIn: '2 days'
|
||||
})
|
||||
return response.status(200).send({
|
||||
token: token.toJSON()
|
||||
@@ -17,10 +14,7 @@ export default class AuthController {
|
||||
}
|
||||
|
||||
public async loginWeb ({ request, auth, response }: HttpContextContract) {
|
||||
const email = request.input('email')
|
||||
const password = request.input('password')
|
||||
const remember = request.input('rembember', false)
|
||||
|
||||
const { email, password, remember } = await request.validate(AuthValidator)
|
||||
await auth.use('web').attempt(email, password, remember)
|
||||
|
||||
return response.status(200).send({
|
||||
@@ -29,16 +23,14 @@ export default class AuthController {
|
||||
}
|
||||
|
||||
public async createInfiniteToken ({ request, auth, response }: HttpContextContract) {
|
||||
const email = request.input('email')
|
||||
const password = request.input('password')
|
||||
const token = await auth.attempt(email, password)
|
||||
const { email, password } = await request.validate(AuthValidator)
|
||||
const token = await auth.use('api').attempt(email, password)
|
||||
return response.status(200).send({
|
||||
token: token.toJSON()
|
||||
})
|
||||
}
|
||||
|
||||
public async logoutApi ({ auth, response }: HttpContextContract) {
|
||||
await auth.use('api').logout()
|
||||
await auth.use('api').revoke()
|
||||
return response.status(200).send({
|
||||
message: 'You have been disconnected!'
|
||||
@@ -47,17 +39,14 @@ export default class AuthController {
|
||||
|
||||
public async logoutWeb ({ auth, response }: HttpContextContract) {
|
||||
await auth.use('web').logout()
|
||||
await auth.use('api').revoke()
|
||||
return response.status(200).send({
|
||||
message: 'You have been disconnected!'
|
||||
})
|
||||
}
|
||||
|
||||
public async user ({ auth, response }: HttpContextContract) {
|
||||
await auth.authenticate()
|
||||
const user = await User.query()
|
||||
.where('id', auth.user!.id)
|
||||
.firstOrFail()
|
||||
public async user ({ auth, response, logger }: HttpContextContract) {
|
||||
const user = await auth.use('web').authenticate() || await auth.use('api').authenticate()
|
||||
logger.info('' + user)
|
||||
return response.status(200).send({
|
||||
user: user
|
||||
})
|
||||
|
||||
@@ -14,7 +14,7 @@ export default class AuthValidator {
|
||||
password: schema.string({ trim: true }, [
|
||||
rules.required()
|
||||
]),
|
||||
remember_me: schema.boolean()
|
||||
remember: schema.boolean.optional()
|
||||
})
|
||||
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
|
||||
@@ -114,8 +114,8 @@ const authConfig: AuthConfig = {
|
||||
*/
|
||||
tokenProvider: {
|
||||
type: 'api',
|
||||
driver: 'redis',
|
||||
redisConnection: 'local',
|
||||
driver: 'database',
|
||||
table: 'api_tokens',
|
||||
foreignKey: 'user_id',
|
||||
},
|
||||
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
/**
|
||||
* Config source: https://git.io/JemcF
|
||||
*
|
||||
* Feel free to let us know via PR, if you find something broken in this config
|
||||
* file.
|
||||
*/
|
||||
|
||||
import Env from '@ioc:Adonis/Core/Env'
|
||||
import { RedisConfig } from '@ioc:Adonis/Addons/Redis'
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Redis configuration
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Following is the configuration used by the Redis provider to connect to
|
||||
| the redis server and execute redis commands.
|
||||
|
|
||||
| Do make sure to pre-define the connections type inside `contracts/redis.ts`
|
||||
| file for AdonisJs to recognize connections.
|
||||
|
|
||||
| Make sure to check `contracts/redis.ts` file for defining extra connections
|
||||
*/
|
||||
const redisConfig: RedisConfig = {
|
||||
connection: Env.get('REDIS_CONNECTION'),
|
||||
|
||||
connections: {
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| The default connection
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The main connection you want to use to execute redis commands. The same
|
||||
| connection will be used by the session provider, if you rely on the
|
||||
| redis driver.
|
||||
|
|
||||
*/
|
||||
local: {
|
||||
host: Env.get('REDIS_HOST'),
|
||||
port: Env.get('REDIS_PORT'),
|
||||
password: Env.get('REDIS_PASSWORD', ''),
|
||||
db: Env.get('REDIS_DB', 0),
|
||||
keyPrefix: 'athena:',
|
||||
healthCheck: true
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export default redisConfig
|
||||
25
database/migrations/1631617499307_api_tokens.ts
Normal file
25
database/migrations/1631617499307_api_tokens.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||
|
||||
export default class ApiTokens extends BaseSchema {
|
||||
protected tableName = 'api_tokens'
|
||||
|
||||
public async up () {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id').primary()
|
||||
table
|
||||
.integer('user_id')
|
||||
.unsigned()
|
||||
.references('users.id')
|
||||
.onDelete('CASCADE')
|
||||
table.string('name').notNullable()
|
||||
table.string('type').notNullable()
|
||||
table.string('token', 64).notNullable()
|
||||
table.timestamp('expires_at', { useTz: true }).nullable()
|
||||
table.timestamp('created_at', { useTz: true }).notNullable()
|
||||
})
|
||||
}
|
||||
|
||||
public async down () {
|
||||
this.schema.dropTable(this.tableName)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@
|
||||
"@adonisjs/core": "^5.1.10",
|
||||
"@adonisjs/lucid": "^15.0.1",
|
||||
"@adonisjs/mail": "^7.2.1",
|
||||
"@adonisjs/redis": "^7.0.6",
|
||||
"@adonisjs/repl": "^3.1.4",
|
||||
"@adonisjs/session": "^6.1.1",
|
||||
"@adonisjs/view": "^6.0.3",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Route from "@ioc:Adonis/Core/Route";
|
||||
|
||||
Route.group(() => {
|
||||
Route.get('/me', 'AuthController.user').middleware('auth')
|
||||
Route.get('/me', 'AuthController.user')
|
||||
Route.post('/token', 'AuthController.createInfiniteToken')
|
||||
|
||||
Route.group(() => {
|
||||
|
||||
@@ -26,7 +26,7 @@ Route.get('/source', async ({response}: HttpContextContract) => {
|
||||
Route.get('/health', async ({response}: HttpContextContract) => {
|
||||
const report = await HealthCheck.getReport()
|
||||
const isLive = await HealthCheck.isLive()
|
||||
const isReady = await HealthCheck.isReady()
|
||||
const isReady = HealthCheck.isReady()
|
||||
return report.healthy ? response.ok({isLive, isReady, report: report.report}) : response.badRequest({
|
||||
isLive,
|
||||
isReady,
|
||||
|
||||
82
yarn.lock
82
yarn.lock
@@ -232,15 +232,6 @@
|
||||
"@poppinss/utils" "^3.1.4"
|
||||
jest-worker "^27.0.6"
|
||||
|
||||
"@adonisjs/redis@^7.0.6":
|
||||
version "7.0.8"
|
||||
resolved "https://registry.yarnpkg.com/@adonisjs/redis/-/redis-7.0.8.tgz#25223b61c253131394ab94aaf29ce12a17b338d9"
|
||||
integrity sha512-5tADsSTAJt3uCxg942/0jjZQkNYkbFNMYfvPczsQ2NX0ixx/CmjHje+RO6o/Uhm7YBcvClMIvJUoFHqa8nnv8w==
|
||||
dependencies:
|
||||
"@poppinss/utils" "^3.1.5"
|
||||
"@types/ioredis" "^4.26.6"
|
||||
ioredis "^4.27.6"
|
||||
|
||||
"@adonisjs/repl@^3.1.4":
|
||||
version "3.1.5"
|
||||
resolved "https://registry.yarnpkg.com/@adonisjs/repl/-/repl-3.1.5.tgz#27eb99f8e5dcc2250435e90b964f12b8d633d8b8"
|
||||
@@ -527,13 +518,6 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz#0ea7b61496902b95890dc4c3a116b60cb8dae812"
|
||||
integrity sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==
|
||||
|
||||
"@types/ioredis@^4.26.6":
|
||||
version "4.26.7"
|
||||
resolved "https://registry.yarnpkg.com/@types/ioredis/-/ioredis-4.26.7.tgz#8c8174b9db38f71f0e372174c66a031a2ca7d9cf"
|
||||
integrity sha512-TOGRR+e1to00CihjgPNygD7+G7ruVnMi62YdIvGUBRfj11k/aWq+Fv5Ea8St0Oy56NngTBfA8GvLn1uvHvhX6Q==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/keyv@*":
|
||||
version "3.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.2.tgz#5d97bb65526c20b6e0845f6b0d2ade4f28604ee5"
|
||||
@@ -1184,11 +1168,6 @@ clone-response@^1.0.2:
|
||||
dependencies:
|
||||
mimic-response "^1.0.0"
|
||||
|
||||
cluster-key-slot@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz#30474b2a981fb12172695833052bc0d01336d10d"
|
||||
integrity sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==
|
||||
|
||||
co-compose@^6.1.4:
|
||||
version "6.1.4"
|
||||
resolved "https://registry.yarnpkg.com/co-compose/-/co-compose-6.1.4.tgz#4e607a29fdda0428c599683b4d65f9c8f9d91723"
|
||||
@@ -1473,11 +1452,6 @@ delegates@^1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
|
||||
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=
|
||||
|
||||
denque@^1.1.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de"
|
||||
integrity sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==
|
||||
|
||||
depd@~1.1.2:
|
||||
version "1.1.2"
|
||||
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
|
||||
@@ -2301,23 +2275,6 @@ interpret@^2.2.0:
|
||||
resolved "https://registry.yarnpkg.com/interpret/-/interpret-2.2.0.tgz#1a78a0b5965c40a5416d007ad6f50ad27c417df9"
|
||||
integrity sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==
|
||||
|
||||
ioredis@^4.27.6:
|
||||
version "4.27.7"
|
||||
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-4.27.7.tgz#11bf2947e23a0e8055931afa7c2da89fc48c8ff3"
|
||||
integrity sha512-lqvFFmUyGIHlrNyDvBoakzy1+ioJzNyoP6CP97GWtdTjWq9IOAnv6l0HUTsqhvd/z9etGgtrDHZ4kWCMAwNkug==
|
||||
dependencies:
|
||||
cluster-key-slot "^1.1.0"
|
||||
debug "^4.3.1"
|
||||
denque "^1.1.0"
|
||||
lodash.defaults "^4.2.0"
|
||||
lodash.flatten "^4.4.0"
|
||||
lodash.isarguments "^3.1.0"
|
||||
p-map "^2.1.0"
|
||||
redis-commands "1.7.0"
|
||||
redis-errors "^1.2.0"
|
||||
redis-parser "^3.0.0"
|
||||
standard-as-callback "^2.1.0"
|
||||
|
||||
ipaddr.js@1.9.1:
|
||||
version "1.9.1"
|
||||
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3"
|
||||
@@ -2665,21 +2622,6 @@ locate-path@^5.0.0:
|
||||
dependencies:
|
||||
p-locate "^4.1.0"
|
||||
|
||||
lodash.defaults@^4.2.0:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
|
||||
integrity sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=
|
||||
|
||||
lodash.flatten@^4.4.0:
|
||||
version "4.4.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
|
||||
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=
|
||||
|
||||
lodash.isarguments@^3.1.0:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
|
||||
integrity sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo=
|
||||
|
||||
lodash@^4.17.15, lodash@^4.17.21:
|
||||
version "4.17.21"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
|
||||
@@ -3582,7 +3524,7 @@ p-locate@^4.1.0:
|
||||
dependencies:
|
||||
p-limit "^2.2.0"
|
||||
|
||||
p-map@^2.0.0, p-map@^2.1.0:
|
||||
p-map@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.1.0.tgz#310928feef9c9ecc65b68b17693018a665cea175"
|
||||
integrity sha512-y3b8Kpd8OAN444hxfBbFfj1FY/RjtTd8tzYwhUqNYXx0fXx2iX4maP4Qr6qhIKbQXI02wTLAda4fYUbDagTUFw==
|
||||
@@ -3940,23 +3882,6 @@ redeyed@~2.1.0:
|
||||
dependencies:
|
||||
esprima "~4.0.0"
|
||||
|
||||
redis-commands@1.7.0:
|
||||
version "1.7.0"
|
||||
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89"
|
||||
integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==
|
||||
|
||||
redis-errors@^1.0.0, redis-errors@^1.2.0:
|
||||
version "1.2.0"
|
||||
resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
|
||||
integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=
|
||||
|
||||
redis-parser@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
|
||||
integrity sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=
|
||||
dependencies:
|
||||
redis-errors "^1.0.0"
|
||||
|
||||
reflect-metadata@^0.1.13:
|
||||
version "0.1.13"
|
||||
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
|
||||
@@ -4360,11 +4285,6 @@ stack-trace@0.0.10:
|
||||
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0"
|
||||
integrity sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=
|
||||
|
||||
standard-as-callback@^2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/standard-as-callback/-/standard-as-callback-2.1.0.tgz#8953fc05359868a77b5b9739a665c5977bb7df45"
|
||||
integrity sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==
|
||||
|
||||
static-extend@^0.1.1:
|
||||
version "0.1.2"
|
||||
resolved "https://registry.yarnpkg.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6"
|
||||
|
||||
Reference in New Issue
Block a user