|  | @@ -1,15 +1,11 @@
 | 
	
		
			
				|  |  |  const auth = require('./auth');
 | 
	
		
			
				|  |  | -require('./db');
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -//let auth = new Auth();
 | 
	
		
			
				|  |  | -let t = auth.generateToken('Loquicom');
 | 
	
		
			
				|  |  | -console.log(t);
 | 
	
		
			
				|  |  | -console.log(auth.verify('Loquicom', t));
 | 
	
		
			
				|  |  | +const db = require('./db');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // Constante d'erreur
 | 
	
		
			
				|  |  |  const ERR_REQUEST = 1;
 | 
	
		
			
				|  |  |  const ERR_AUTH = 2;
 | 
	
		
			
				|  |  | -const ERR_TOKEN = 3;
 | 
	
		
			
				|  |  | +const ERR_UNKNOW = 3;
 | 
	
		
			
				|  |  | +const ERR_TOKEN = 4;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // Fonctions de traitement pour les routes
 | 
	
		
			
				|  |  |  function verify(req, res, next) {
 | 
	
	
		
			
				|  | @@ -29,6 +25,15 @@ function error(code) {
 | 
	
		
			
				|  |  |          case ERR_REQUEST:
 | 
	
		
			
				|  |  |              answer.message = 'Bad request';
 | 
	
		
			
				|  |  |              break;
 | 
	
		
			
				|  |  | +        case ERR_AUTH:
 | 
	
		
			
				|  |  | +            answer.message = 'Bad authentication';
 | 
	
		
			
				|  |  | +            break;
 | 
	
		
			
				|  |  | +        case ERR_UNKNOW:
 | 
	
		
			
				|  |  | +            answer.message = 'Unknow user';
 | 
	
		
			
				|  |  | +            break;
 | 
	
		
			
				|  |  | +        case ERR_TOKEN:
 | 
	
		
			
				|  |  | +            answer.message = 'Invalid token';
 | 
	
		
			
				|  |  | +            break;
 | 
	
		
			
				|  |  |          default:
 | 
	
		
			
				|  |  |              answer.message = 'Unknow error';
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -48,11 +53,47 @@ function success(data) {
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // Definition des routes
 | 
	
		
			
				|  |  | -app.get('/authentication', function(req, res) {
 | 
	
		
			
				|  |  | -    console.log(success({authentication: auth.isActivated()}));
 | 
	
		
			
				|  |  | +app.get('/authentication', (req, res) => {
 | 
	
		
			
				|  |  |      res.json(success({authentication: auth.isActivated()}));
 | 
	
		
			
				|  |  |  });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +app.post('/register', (req, res) => {
 | 
	
		
			
				|  |  | +    if(req.body.user === undefined || req.body.password === undefined) {
 | 
	
		
			
				|  |  | +        res.json(error(ERR_REQUEST));
 | 
	
		
			
				|  |  | +        return;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    const passHash = auth.passwordHash(req.body.password);
 | 
	
		
			
				|  |  | +    db.addUser(req.body.user, passHash);
 | 
	
		
			
				|  |  | +    return res.json(success());
 | 
	
		
			
				|  |  | +});
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +app.post('/login', (req, res) => {
 | 
	
		
			
				|  |  | +    if(req.body.user === undefined || req.body.password === undefined) {
 | 
	
		
			
				|  |  | +        res.json(error(ERR_REQUEST));
 | 
	
		
			
				|  |  | +        return;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    db.getUser(req.body.user).then((user) => {
 | 
	
		
			
				|  |  | +        if(user === undefined) {
 | 
	
		
			
				|  |  | +            res.json(error(ERR_UNKNOW));
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            if(auth.passwordVerify(req.body.password, user.pass)) {
 | 
	
		
			
				|  |  | +                res.json(success({token: auth.generateToken(req.body.user)}));
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                res.json(error(ERR_AUTH));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +});
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +app.post('/token', (req, res) => {
 | 
	
		
			
				|  |  | +    if(req.body.user === undefined || req.body.token === undefined) {
 | 
	
		
			
				|  |  | +        res.json(error(ERR_REQUEST));
 | 
	
		
			
				|  |  | +        return;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    res.json(success({valid: auth.verify(req.body.user, req.body.token)}));
 | 
	
		
			
				|  |  | +});
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  |  app.get('/', function (req, res) {
 | 
	
		
			
				|  |  |      res.send('Hello World!');
 | 
	
		
			
				|  |  |  });
 | 
	
	
		
			
				|  | @@ -63,4 +104,5 @@ app.get('/test/:val?', function (req, res) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  app.get(/.*aze$/, function (req, res) {
 | 
	
		
			
				|  |  |      res.send('URL end with aze');
 | 
	
		
			
				|  |  | -})
 | 
	
		
			
				|  |  | +})
 | 
	
		
			
				|  |  | +*/
 |