mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-31 11:57:50 +01:00
Working and fixing form
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||||
import User from "App/Models/User";
|
import User from "App/Models/User";
|
||||||
import AuthValidator from "App/Validators/AuthValidator";
|
import AuthValidator from "App/Validators/AuthValidator";
|
||||||
|
import {AllyUserContract} from "@ioc:Adonis/Addons/Ally";
|
||||||
|
|
||||||
export default class AuthController {
|
export default class AuthController {
|
||||||
|
|
||||||
@@ -54,61 +55,82 @@ export default class AuthController {
|
|||||||
|
|
||||||
public async user ({auth}: HttpContextContract) {
|
public async user ({auth}: HttpContextContract) {
|
||||||
await auth.authenticate()
|
await auth.authenticate()
|
||||||
const user = await User.query()
|
return await User.query()
|
||||||
.where('id', auth.user!.id)
|
.where('id', auth.user!.id)
|
||||||
.firstOrFail()
|
.firstOrFail()
|
||||||
return { user }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async twitter ({ally, auth}: HttpContextContract) {
|
public async twitter ({ally, auth}: HttpContextContract) {
|
||||||
const twitter = ally.use('twitter')
|
const twitter = ally.use('twitter')
|
||||||
|
|
||||||
|
if (twitter.accessDenied()) {
|
||||||
|
return 'Access Denied'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (twitter.stateMisMatch()) {
|
||||||
|
return 'Request expired. Retry again'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (twitter.hasError()) {
|
||||||
|
return twitter.getError()
|
||||||
|
}
|
||||||
|
|
||||||
const twitterUser = await twitter.user()
|
const twitterUser = await twitter.user()
|
||||||
|
const user = await this.createUser(twitterUser)
|
||||||
const user = await User.firstOrCreate({
|
|
||||||
email: twitterUser.email,
|
|
||||||
}, {
|
|
||||||
email: twitterUser.email,
|
|
||||||
username: twitterUser.name,
|
|
||||||
isConfirmed: twitterUser.emailVerificationState === 'verified'
|
|
||||||
})
|
|
||||||
|
|
||||||
await auth.use('web').login(user)
|
await auth.use('web').login(user)
|
||||||
|
return user
|
||||||
return { user }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async github ({ally, auth}: HttpContextContract) {
|
public async github ({ally, auth}: HttpContextContract) {
|
||||||
const github = ally.use('github')
|
const github = ally.use('github')
|
||||||
|
|
||||||
|
if (github.accessDenied()) {
|
||||||
|
return 'Access Denied'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (github.stateMisMatch()) {
|
||||||
|
return 'Request expired. Retry again'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (github.hasError()) {
|
||||||
|
return github.getError()
|
||||||
|
}
|
||||||
|
|
||||||
const githubUser = await github.user()
|
const githubUser = await github.user()
|
||||||
|
const user = await this.createUser(githubUser)
|
||||||
const user = await User.firstOrCreate({
|
|
||||||
email: githubUser.email,
|
|
||||||
}, {
|
|
||||||
email: githubUser.email,
|
|
||||||
username: githubUser.name,
|
|
||||||
isConfirmed: githubUser.emailVerificationState === 'verified'
|
|
||||||
})
|
|
||||||
|
|
||||||
await auth.use('web').login(user)
|
await auth.use('web').login(user)
|
||||||
|
return user
|
||||||
return { user }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async google ({ally, auth}: HttpContextContract) {
|
public async google ({ally, auth}: HttpContextContract) {
|
||||||
const google = ally.use('google')
|
const google = ally.use('google')
|
||||||
|
|
||||||
|
if (google.accessDenied()) {
|
||||||
|
return 'Access Denied'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (google.stateMisMatch()) {
|
||||||
|
return 'Request expired. Retry again'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (google.hasError()) {
|
||||||
|
return google.getError()
|
||||||
|
}
|
||||||
|
|
||||||
const googleUser = await google.user()
|
const googleUser = await google.user()
|
||||||
|
const user = await this.createUser(googleUser)
|
||||||
const user = await User.firstOrCreate({
|
|
||||||
email: googleUser.email,
|
|
||||||
}, {
|
|
||||||
email: googleUser.email,
|
|
||||||
username: googleUser.name,
|
|
||||||
isConfirmed: googleUser.emailVerificationState === 'verified'
|
|
||||||
})
|
|
||||||
|
|
||||||
await auth.use('web').login(user)
|
await auth.use('web').login(user)
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
return { user }
|
public async createUser(allyUser: AllyUserContract<any>): Promise<User> {
|
||||||
|
return await User.firstOrCreate({
|
||||||
|
email: allyUser.email!,
|
||||||
|
}, {
|
||||||
|
email: allyUser.email!,
|
||||||
|
username: allyUser.name,
|
||||||
|
isConfirmed: allyUser.emailVerificationState === 'verified'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import FormValidator from "App/Validators/FormValidator";
|
import FormValidator from "App/Validators/FormValidator";
|
||||||
import Form from "App/Models/Form";
|
import Form from "App/Models/Form";
|
||||||
import FormConfirmation from "App/Mailers/FormConfirmation";
|
//import FormConfirmation from "App/Mailers/FormConfirmation";
|
||||||
|
|
||||||
export default class FormsController {
|
export default class FormsController {
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ export default class FormsController {
|
|||||||
|
|
||||||
await Form.create(data)
|
await Form.create(data)
|
||||||
|
|
||||||
await new FormConfirmation(data.name, data.email).sendLater()
|
//await new FormConfirmation(data.name, data.email).sendLater()
|
||||||
//todo send confirmation email + email to me
|
//todo send confirmation email + email to me
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
status: 200
|
status: 200
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const allyConfig: AllyConfig = {
|
|||||||
driver: 'github',
|
driver: 'github',
|
||||||
clientId: Env.get('GITHUB_CLIENT_ID'),
|
clientId: Env.get('GITHUB_CLIENT_ID'),
|
||||||
clientSecret: Env.get('GITHUB_CLIENT_SECRET'),
|
clientSecret: Env.get('GITHUB_CLIENT_SECRET'),
|
||||||
callbackUrl: 'http://localhost:5555/github',
|
callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/github`,
|
||||||
},
|
},
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@@ -38,7 +38,7 @@ const allyConfig: AllyConfig = {
|
|||||||
driver: 'twitter',
|
driver: 'twitter',
|
||||||
clientId: Env.get('TWITTER_CLIENT_ID'),
|
clientId: Env.get('TWITTER_CLIENT_ID'),
|
||||||
clientSecret: Env.get('TWITTER_CLIENT_SECRET'),
|
clientSecret: Env.get('TWITTER_CLIENT_SECRET'),
|
||||||
callbackUrl: 'http://localhost:5555/twitter',
|
callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/twitter`,
|
||||||
},
|
},
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@@ -49,7 +49,7 @@ const allyConfig: AllyConfig = {
|
|||||||
driver: 'google',
|
driver: 'google',
|
||||||
clientId: Env.get('GOOGLE_CLIENT_ID'),
|
clientId: Env.get('GOOGLE_CLIENT_ID'),
|
||||||
clientSecret: Env.get('GOOGLE_CLIENT_SECRET'),
|
clientSecret: Env.get('GOOGLE_CLIENT_SECRET'),
|
||||||
callbackUrl: 'http://localhost:5555/google',
|
callbackUrl: `${Env.get('HOST')}:${Env.get('PORT')}/auth/google`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
32
package.json
32
package.json
@@ -11,30 +11,30 @@
|
|||||||
"lr": "node ace list:routes"
|
"lr": "node ace list:routes"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@adonisjs/assembler": "^5.1.1",
|
"@adonisjs/assembler": "^5.3.2",
|
||||||
"adonis-preset-ts": "^2.1.0",
|
"adonis-preset-ts": "^2.1.0",
|
||||||
"pino-pretty": "^4.7.1",
|
"pino-pretty": "^5.0.2",
|
||||||
"typescript": "^4.2.4",
|
"typescript": "^4.3.4",
|
||||||
"youch": "^2.2.2",
|
"youch": "^2.2.2",
|
||||||
"youch-terminal": "^1.1.1"
|
"youch-terminal": "^1.1.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@adonisjs/ally": "^3.2.1",
|
"@adonisjs/ally": "^4.0.2",
|
||||||
"@adonisjs/auth": "^8.0.2",
|
"@adonisjs/auth": "^8.0.6",
|
||||||
"@adonisjs/core": "~5.1.6",
|
"@adonisjs/core": "~5.1.8",
|
||||||
"@adonisjs/lucid": "^14.0.0",
|
"@adonisjs/lucid": "^15.0.1",
|
||||||
"@adonisjs/mail": "^7.1.1",
|
"@adonisjs/mail": "^7.2.1",
|
||||||
"@adonisjs/redis": "^7.0.2",
|
"@adonisjs/redis": "^7.0.6",
|
||||||
"@adonisjs/repl": "^3.1.2",
|
"@adonisjs/repl": "^3.1.4",
|
||||||
"@adonisjs/session": "^6.0.3",
|
"@adonisjs/session": "^6.0.6",
|
||||||
"@adonisjs/view": "^6.0.1",
|
"@adonisjs/view": "^6.0.3",
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"luxon": "^1.26.0",
|
"luxon": "^1.27.0",
|
||||||
"mjml": "^4.9.3",
|
"mjml": "^4.10.0",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"phc-argon2": "^1.1.1",
|
"phc-argon2": "^1.1.1",
|
||||||
"proxy-addr": "^2.0.6",
|
"proxy-addr": "^2.0.7",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"tslib": "^2.2.0"
|
"tslib": "^2.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1397
pnpm-lock.yaml
generated
1397
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -78,7 +78,17 @@ Route.group(() => {
|
|||||||
Route.post('/api/login', 'AuthController.loginApi')
|
Route.post('/api/login', 'AuthController.loginApi')
|
||||||
Route.post('/api/logout', 'AuthController.logoutApi')
|
Route.post('/api/logout', 'AuthController.logoutApi')
|
||||||
|
|
||||||
Route.get('/twitter', 'AuthController.twitter')
|
Route.get('/twitter/callback', 'AuthController.twitter')
|
||||||
Route.get('/github', 'AuthController.github')
|
Route.get('/github/callback', 'AuthController.github')
|
||||||
Route.get('/google', 'AuthController.google')
|
Route.get('/google/callback', 'AuthController.google')
|
||||||
|
|
||||||
|
Route.get('/twitter/redirect', async ({ ally}) => {
|
||||||
|
return ally.use('twitter').redirect()
|
||||||
|
})
|
||||||
|
Route.get('/github/redirect', async ({ ally}) => {
|
||||||
|
return ally.use('github').redirect()
|
||||||
|
})
|
||||||
|
Route.get('/google/redirect', async ({ ally}) => {
|
||||||
|
return ally.use('google').redirect()
|
||||||
|
})
|
||||||
}).prefix('auth')
|
}).prefix('auth')
|
||||||
|
|||||||
Reference in New Issue
Block a user