-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from datasci4health/development
Development
- Loading branch information
Showing
7 changed files
with
88 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,97 @@ | ||
/* global use */ | ||
'use strict' | ||
|
||
const Logger = use('Logger') | ||
// const Logger = use('Logger') | ||
|
||
const Env = use('Env') | ||
const axios = use('axios'); | ||
var FormData = use('form-data'); | ||
const axios = use('axios') | ||
// var FormData = use('form-data') | ||
|
||
const { validate } = use('Validator') | ||
|
||
const User = use('App/Models/User'); | ||
// const User = use('App/Models/User') | ||
|
||
class AuthController { | ||
|
||
create({ view, session }){ | ||
create ({ view, session }) { | ||
return view.render('registration.login', { pageTitle: 'Log in' }) | ||
} | ||
|
||
|
||
|
||
async login({ view, request, session, response, auth }) { | ||
try{ | ||
const params = request.all() | ||
|
||
const messages = { | ||
'email.required': 'Missing email', | ||
'password.required': 'Missing password', | ||
} | ||
|
||
const validation = await validate(params, { | ||
email: 'required', | ||
password: 'required', | ||
}, messages) | ||
|
||
// * If validation fails, early returns with validation message | ||
if (validation.fails()) { | ||
session | ||
.withErrors(validation.messages()) | ||
.flashExcept(['password']) | ||
|
||
return response.redirect('back') | ||
} | ||
|
||
let endpoint_url = Env.get("HARENA_MANAGER_URL") + "/api/v1/auth/login" | ||
|
||
var config = { | ||
method: 'post', | ||
url: endpoint_url, | ||
data: { | ||
email: params.email, | ||
password: params.password, | ||
} | ||
}; | ||
|
||
await axios(config) | ||
.then(async function (endpoint_response) { | ||
|
||
let response_user = endpoint_response.data | ||
|
||
await auth.loginViaId(response_user.id) | ||
response.cookie('token', response_user.token) | ||
|
||
return response.route('index') | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
} catch (e){ | ||
console.log(e) | ||
} | ||
async login ({ view, request, session, response, auth }) { | ||
try { | ||
const params = request.all() | ||
|
||
const messages = { | ||
'email.required': 'Missing email', | ||
'password.required': 'Missing password', | ||
} | ||
|
||
const validation = await validate(params, { | ||
email: 'required', | ||
password: 'required' | ||
}, messages) | ||
|
||
// * If validation fails, early returns with validation message | ||
if (validation.fails()) { | ||
session | ||
.withErrors(validation.messages()) | ||
.flashExcept(['password']) | ||
|
||
return response.redirect('back') | ||
} | ||
|
||
const endpointUrl = Env.get('HARENA_MANAGER_URL') + '/api/v1/auth/login' | ||
|
||
var config = { | ||
method: 'post', | ||
url: endpointUrl, | ||
data: { | ||
email: params.email, | ||
password: params.password | ||
} | ||
} | ||
|
||
await axios(config) | ||
.then(async function (endpointResponse) { | ||
|
||
const responseUser = endpointResponse.data | ||
|
||
await auth.loginViaId(responseUser.id) | ||
response.cookie('token', responseUser.token) | ||
|
||
return response.route('index') | ||
}) | ||
.catch(function (error) { | ||
console.log(error) | ||
}) | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
|
||
|
||
async logout({ session, auth, response, request }){ | ||
try{ | ||
const endpoint_url = Env.get("HARENA_MANAGER_URL") + "/api/v1/auth/logout" | ||
|
||
var config = { | ||
method: 'post', | ||
url: endpoint_url, | ||
headers: { | ||
Authorization: 'Bearer ' + request.cookie('token') | ||
} | ||
}; | ||
|
||
await axios(config) | ||
.then(async function (endpoint_response) { | ||
await auth.logout() | ||
return response.route('index') | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
}catch (e){ | ||
console.log(e) | ||
} | ||
async logout ({ session, auth, response, request }) { | ||
try { | ||
const endpointUrl = Env.get('HARENA_MANAGER_URL') + '/api/v1/auth/logout' | ||
|
||
var config = { | ||
method: 'post', | ||
url: endpointUrl, | ||
headers: { | ||
Authorization: 'Bearer ' + request.cookie('token') | ||
} | ||
} | ||
|
||
await axios(config) | ||
.then(async function (endpointResponse) { | ||
await auth.logout() | ||
return response.route('index') | ||
}) | ||
.catch(function (error) { | ||
console.log(error) | ||
}) | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
module.exports = AuthController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,9 @@ | |
"App": "./app" | ||
}, | ||
"standard": { | ||
"globals": ["use"], | ||
"globals": [ | ||
"use" | ||
], | ||
"ignore": [ | ||
"public/" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters