-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
45 lines (43 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const session = require('./session');
const render = require('./render');
const authentication = require('./authentication');
module.exports = {
get: (event, context) => {
const sess = session.getSession(event.headers);
if (sess.valid) {
render.user({
username: sess.user.username,
first: sess.user.first,
last: sess.user.last
}, (err, res) => {
if (err) { return context.done(err); }
return context.done(null, res);
});
} else {
render.login((err, res) => {
if (err) { return context.done(err); }
return context.done(null, res);
});
}
},
login: (event, context) => {
const username = event.data.username;
const pass = event.data.password;
const authRes = authentication.auth(username, pass);
if (authRes.success) {
const sess = session.setSession(authRes.user);
return context.done(null, {
success: authRes.success,
Cookie: sess.Cookie
});
} else {
return context.done(null, authRes)
}
},
logout: (event, context) => {
const sessionRes = session.getSession(event.headers);
const sess = session.destroySession(sessionRes.user);
context.done(null, { Cookie: sess.Cookie });
}
};