Skip to content

Commit

Permalink
draft for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbedouret committed Jan 11, 2018
1 parent 340a6c7 commit 65b569a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
28 changes: 27 additions & 1 deletion api/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var User = require('../models/User');
var mailing = require('../mail');
var nev = mailing('en');
var auth = require("../helpers/auth");

module.exports = {
createUser: createUser,
activateUser: activateUser,
listUser: listUser,
removeUser: removeUser,
getUser: getUser,
updateUser: updateUser
updateUser: updateUser,
loginUser: loginUser
};

function createUser(req, res) {
Expand Down Expand Up @@ -141,4 +143,28 @@ function updateUser(req, res) {
});
return res.status(200).json(users);
});
}
function loginUser(args, res) {
var role = args.swagger.params.role.value;
var username = args.body.username;
var password = args.body.password;
console.log(role + username + password);

if (role != "user" && role != "admin") {
return res.status(400).json({
message: 'Error: Role must be either "admin" or "user"'
});
}

if (username == "username" && password == "password" && role) {
var tokenString = auth.issueToken(username, role);
res.status(200).json({
token: tokenString,
message: "User successfully authenticated"
});
} else {
res.status(403).json({
message: "Error: Credentials incorrect"
});
}
}
28 changes: 23 additions & 5 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,28 @@ paths:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/user/login:
/user/login/{role}:
x-swagger-router-controller: user
post:
operationId: login
operationId: loginUser
description: Authenticate user by email and password
parameters:
- $ref: '#/parameters/User'
- name: role
description: user role
in: path
required: true
type: string
- name: info
description: User properties
in: body
required: true
schema:
$ref: "#/definitions/User"
responses:
"200":
description: Success
schema:
$ref: "#/definitions/GeneralResponse"
$ref: "#/definitions/LoginResponse"
default:
description: Error
schema:
Expand Down Expand Up @@ -263,4 +273,12 @@ definitions:
type: string
locale:
type: string

LoginResponse:
required:
- token
- message
properties:
token:
type: string
message:
type: string

0 comments on commit 65b569a

Please sign in to comment.