mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Working and fixing form
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from "App/Models/User";
|
||||
import AuthValidator from "App/Validators/AuthValidator";
|
||||
import {AllyUserContract} from "@ioc:Adonis/Addons/Ally";
|
||||
|
||||
export default class AuthController {
|
||||
|
||||
@@ -54,61 +55,82 @@ export default class AuthController {
|
||||
|
||||
public async user ({auth}: HttpContextContract) {
|
||||
await auth.authenticate()
|
||||
const user = await User.query()
|
||||
return await User.query()
|
||||
.where('id', auth.user!.id)
|
||||
.firstOrFail()
|
||||
return { user }
|
||||
}
|
||||
|
||||
public async twitter ({ally, auth}: HttpContextContract) {
|
||||
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 user = await User.firstOrCreate({
|
||||
email: twitterUser.email,
|
||||
}, {
|
||||
email: twitterUser.email,
|
||||
username: twitterUser.name,
|
||||
isConfirmed: twitterUser.emailVerificationState === 'verified'
|
||||
})
|
||||
|
||||
const user = await this.createUser(twitterUser)
|
||||
await auth.use('web').login(user)
|
||||
|
||||
return { user }
|
||||
return user
|
||||
}
|
||||
|
||||
public async github ({ally, auth}: HttpContextContract) {
|
||||
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 user = await User.firstOrCreate({
|
||||
email: githubUser.email,
|
||||
}, {
|
||||
email: githubUser.email,
|
||||
username: githubUser.name,
|
||||
isConfirmed: githubUser.emailVerificationState === 'verified'
|
||||
})
|
||||
|
||||
const user = await this.createUser(githubUser)
|
||||
await auth.use('web').login(user)
|
||||
|
||||
return { user }
|
||||
return user
|
||||
}
|
||||
|
||||
public async google ({ally, auth}: HttpContextContract) {
|
||||
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 user = await User.firstOrCreate({
|
||||
email: googleUser.email,
|
||||
}, {
|
||||
email: googleUser.email,
|
||||
username: googleUser.name,
|
||||
isConfirmed: googleUser.emailVerificationState === 'verified'
|
||||
})
|
||||
|
||||
const user = await this.createUser(googleUser)
|
||||
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 FormValidator from "App/Validators/FormValidator";
|
||||
import Form from "App/Models/Form";
|
||||
import FormConfirmation from "App/Mailers/FormConfirmation";
|
||||
//import FormConfirmation from "App/Mailers/FormConfirmation";
|
||||
|
||||
export default class FormsController {
|
||||
|
||||
@@ -10,7 +10,7 @@ export default class FormsController {
|
||||
|
||||
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
|
||||
return response.status(200).send({
|
||||
status: 200
|
||||
|
||||
@@ -27,7 +27,7 @@ const allyConfig: AllyConfig = {
|
||||
driver: 'github',
|
||||
clientId: Env.get('GITHUB_CLIENT_ID'),
|
||||
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',
|
||||
clientId: Env.get('TWITTER_CLIENT_ID'),
|
||||
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',
|
||||
clientId: Env.get('GOOGLE_CLIENT_ID'),
|
||||
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"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@adonisjs/assembler": "^5.1.1",
|
||||
"@adonisjs/assembler": "^5.3.2",
|
||||
"adonis-preset-ts": "^2.1.0",
|
||||
"pino-pretty": "^4.7.1",
|
||||
"typescript": "^4.2.4",
|
||||
"pino-pretty": "^5.0.2",
|
||||
"typescript": "^4.3.4",
|
||||
"youch": "^2.2.2",
|
||||
"youch-terminal": "^1.1.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@adonisjs/ally": "^3.2.1",
|
||||
"@adonisjs/auth": "^8.0.2",
|
||||
"@adonisjs/core": "~5.1.6",
|
||||
"@adonisjs/lucid": "^14.0.0",
|
||||
"@adonisjs/mail": "^7.1.1",
|
||||
"@adonisjs/redis": "^7.0.2",
|
||||
"@adonisjs/repl": "^3.1.2",
|
||||
"@adonisjs/session": "^6.0.3",
|
||||
"@adonisjs/view": "^6.0.1",
|
||||
"@adonisjs/ally": "^4.0.2",
|
||||
"@adonisjs/auth": "^8.0.6",
|
||||
"@adonisjs/core": "~5.1.8",
|
||||
"@adonisjs/lucid": "^15.0.1",
|
||||
"@adonisjs/mail": "^7.2.1",
|
||||
"@adonisjs/redis": "^7.0.6",
|
||||
"@adonisjs/repl": "^3.1.4",
|
||||
"@adonisjs/session": "^6.0.6",
|
||||
"@adonisjs/view": "^6.0.3",
|
||||
"axios": "^0.21.1",
|
||||
"luxon": "^1.26.0",
|
||||
"mjml": "^4.9.3",
|
||||
"luxon": "^1.27.0",
|
||||
"mjml": "^4.10.0",
|
||||
"mysql": "^2.18.1",
|
||||
"phc-argon2": "^1.1.1",
|
||||
"proxy-addr": "^2.0.6",
|
||||
"proxy-addr": "^2.0.7",
|
||||
"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/logout', 'AuthController.logoutApi')
|
||||
|
||||
Route.get('/twitter', 'AuthController.twitter')
|
||||
Route.get('/github', 'AuthController.github')
|
||||
Route.get('/google', 'AuthController.google')
|
||||
Route.get('/twitter/callback', 'AuthController.twitter')
|
||||
Route.get('/github/callback', 'AuthController.github')
|
||||
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')
|
||||
|
||||
Reference in New Issue
Block a user