Skip to content

Commit 9b3d9fb

Browse files
committed
Add initial auth module implementation
1 parent 61b4395 commit 9b3d9fb

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

api/.eslintrc.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
"globals": {
66
"api": "readonly",
77
"db": "readonly",
8-
"common": "readonly"
8+
"common": "readonly",
9+
"domain": "readonly",
10+
"lib": "readonly",
11+
"context": "readonly",
12+
"config": "readonly",
13+
"metarhia": "readonly"
914
},
1015
"rules": {
1116
"strict": ["error", "never"],

api/auth/provider.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
({
2+
generateToken() {
3+
const { characters, secret, length } = config.sessions;
4+
return metarhia.metautil.generateToken(secret, characters, length);
5+
},
6+
7+
saveSession(token, data) {
8+
db.pg.update('Session', { data: JSON.stringify(data) }, { token });
9+
},
10+
11+
startSession(token, data, fields = {}) {
12+
const record = { token, data: JSON.stringify(data), ...fields };
13+
db.pg.insert('Session', record);
14+
},
15+
16+
async restoreSession(token) {
17+
const record = await db.pg.row('Session', ['data'], { token });
18+
if (record && record.data) return record.data;
19+
return null;
20+
},
21+
22+
deleteSession(token) {
23+
db.pg.delete('Session', { token });
24+
},
25+
26+
async registerUser(login, password) {
27+
return db.pg.insert('Account', { login, password });
28+
},
29+
30+
async getUser(login) {
31+
return db.pg.row('Account', { login });
32+
},
33+
});

api/auth/register.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
({
2+
access: 'public',
3+
method: async ({ login, password, fullName }) => {
4+
const hash = await metarhia.metautil.hashPassword(password);
5+
await api.auth.provider.registerUser(login, hash, fullName);
6+
const token = await context.client.startSession();
7+
return { status: 'success', token };
8+
},
9+
});

api/auth/restore.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
async ({ token }) => {
2-
console.log({ method: 'auth.restore', token });
3-
return { status: 'ok' };
4-
};
1+
({
2+
access: 'public',
3+
method: async ({ token }) => {
4+
const restored = context.client.restoreSession(token);
5+
if (restored) return { status: 'logged' };
6+
const data = await api.auth.provider.restoreSession(token);
7+
if (!data) return { status: 'not logged' };
8+
context.client.startSession(token, data);
9+
return { status: 'logged' };
10+
},
11+
});

api/auth/signin.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
({
22
access: 'public',
33
method: async ({ login, password }) => {
4-
console.log({ method: 'auth.signin', login, password });
5-
return { status: 'ok', token: '--no-token-provided--' };
4+
const user = await api.auth.provider.getUser(login);
5+
if (!user) throw new Error('Incorrect login or password');
6+
const { accountId, password: hash } = user;
7+
const valid = await metarhia.metautil.validatePassword(password, hash);
8+
if (!valid) throw new Error('Incorrect login or password');
9+
console.log(`Logged user: ${login}`);
10+
const token = api.auth.provider.generateToken();
11+
const data = { accountId: user.accountId };
12+
context.client.startSession(token, data);
13+
const { ip } = context.client;
14+
api.auth.provider.startSession(token, data, { ip, accountId });
15+
return { status: 'logged', token };
616
},
717
});

lib/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)