@proteinjs/user-server 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/generated/index.d.ts +11 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/index.js +45 -0
- package/dist/generated/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/src/authentication/DbSessionStore.d.ts +17 -0
- package/dist/src/authentication/DbSessionStore.d.ts.map +1 -0
- package/dist/src/authentication/DbSessionStore.js +181 -0
- package/dist/src/authentication/DbSessionStore.js.map +1 -0
- package/dist/src/authentication/DefaultAdminCredentials.d.ts +12 -0
- package/dist/src/authentication/DefaultAdminCredentials.d.ts.map +1 -0
- package/dist/src/authentication/DefaultAdminCredentials.js +18 -0
- package/dist/src/authentication/DefaultAdminCredentials.js.map +1 -0
- package/dist/src/authentication/authenticate.d.ts +6 -0
- package/dist/src/authentication/authenticate.d.ts.map +1 -0
- package/dist/src/authentication/authenticate.js +79 -0
- package/dist/src/authentication/authenticate.js.map +1 -0
- package/dist/src/authentication/destroySession.d.ts +2 -0
- package/dist/src/authentication/destroySession.d.ts.map +1 -0
- package/dist/src/authentication/destroySession.js +58 -0
- package/dist/src/authentication/destroySession.js.map +1 -0
- package/dist/src/authorization/userCache.d.ts +4 -0
- package/dist/src/authorization/userCache.d.ts.map +1 -0
- package/dist/src/authorization/userCache.js +70 -0
- package/dist/src/authorization/userCache.js.map +1 -0
- package/dist/src/routes/createUser.d.ts +3 -0
- package/dist/src/routes/createUser.d.ts.map +1 -0
- package/dist/src/routes/createUser.js +83 -0
- package/dist/src/routes/createUser.js.map +1 -0
- package/dist/src/routes/login.d.ts +3 -0
- package/dist/src/routes/login.d.ts.map +1 -0
- package/dist/src/routes/login.js +76 -0
- package/dist/src/routes/login.js.map +1 -0
- package/dist/src/routes/logout.d.ts +3 -0
- package/dist/src/routes/logout.d.ts.map +1 -0
- package/dist/src/routes/logout.js +87 -0
- package/dist/src/routes/logout.js.map +1 -0
- package/generated/index.ts +41 -0
- package/index.ts +2 -0
- package/jest.config.js +18 -0
- package/package.json +43 -0
- package/src/authentication/DbSessionStore.ts +91 -0
- package/src/authentication/DefaultAdminCredentials.ts +15 -0
- package/src/authentication/authenticate.ts +28 -0
- package/src/authentication/destroySession.ts +9 -0
- package/src/authorization/userCache.ts +23 -0
- package/src/routes/createUser.ts +32 -0
- package/src/routes/login.ts +29 -0
- package/src/routes/logout.ts +28 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import { SessionDataCache } from '@proteinjs/server-api';
|
|
3
|
+
import { getSystemDb } from '@proteinjs/db';
|
|
4
|
+
import { User, tables, guestUser, USER_SESSION_CACHE_KEY } from '@proteinjs/user';
|
|
5
|
+
import { DefaultAdminCredentials } from '../authentication/DefaultAdminCredentials';
|
|
6
|
+
|
|
7
|
+
export const userCache: SessionDataCache<User> = {
|
|
8
|
+
key: USER_SESSION_CACHE_KEY,
|
|
9
|
+
create: async (sessionId: string, userEmail: string): Promise<User> => {
|
|
10
|
+
let user = guestUser;
|
|
11
|
+
if (userEmail) {
|
|
12
|
+
const adminCredentials = DefaultAdminCredentials.getCredentials();
|
|
13
|
+
if (adminCredentials && userEmail == adminCredentials.username) {
|
|
14
|
+
const adminUser: User = { name: 'Admin', email: adminCredentials.username, password: adminCredentials.password, emailVerified: true, roles: 'admin', created: moment(), updated: moment(), id: 'admin' };
|
|
15
|
+
user = adminUser;
|
|
16
|
+
} else {
|
|
17
|
+
user = await getSystemDb().get(tables.User, { email: userEmail });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return user;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import sha256 from 'crypto-js/sha256';
|
|
2
|
+
import { Route } from '@proteinjs/server-api';
|
|
3
|
+
import { getSystemDb } from '@proteinjs/db';
|
|
4
|
+
import { User, tables, routes } from '@proteinjs/user';
|
|
5
|
+
import { Logger } from '@proteinjs/util';
|
|
6
|
+
|
|
7
|
+
export const createUser: Route = {
|
|
8
|
+
path: routes.createUser.path,
|
|
9
|
+
method: routes.createUser.method,
|
|
10
|
+
onRequest: async (request, response): Promise<void> => {
|
|
11
|
+
const logger = new Logger('createUser');
|
|
12
|
+
const user = request.body as User;
|
|
13
|
+
const db = getSystemDb();
|
|
14
|
+
const userRecord = await db.get(tables.User, { email: user.email });
|
|
15
|
+
if (userRecord) {
|
|
16
|
+
const error = `User with this email already exists`;
|
|
17
|
+
console.error(`${error}: ${user.email}`);
|
|
18
|
+
response.send({ error });
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
await db.insert(tables.User, {
|
|
23
|
+
name: user.name,
|
|
24
|
+
email: user.email,
|
|
25
|
+
password: sha256(user.password).toString(),
|
|
26
|
+
emailVerified: false,
|
|
27
|
+
roles: '',
|
|
28
|
+
});
|
|
29
|
+
logger.info(`Created user: ${user.email}`);
|
|
30
|
+
response.send({});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Route } from '@proteinjs/server-api';
|
|
2
|
+
import { routes } from '@proteinjs/user';
|
|
3
|
+
import { authenticate } from '../authentication/authenticate';
|
|
4
|
+
|
|
5
|
+
export const login: Route = {
|
|
6
|
+
path: routes.login.path,
|
|
7
|
+
method: routes.login.method,
|
|
8
|
+
onRequest: async (request: any, response): Promise<void> => {
|
|
9
|
+
const credentials: {email: string, password: string} = request.body;
|
|
10
|
+
if (!credentials.email || !credentials.password) {
|
|
11
|
+
const error = `Email and password cannot be blank`;
|
|
12
|
+
console.error(error);
|
|
13
|
+
response.send({ error });
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const result = await authenticate(credentials.email, credentials.password);
|
|
18
|
+
if (result !== true) {
|
|
19
|
+
console.error(result);
|
|
20
|
+
response.send({ error: result });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
await new Promise((resolve, reject) => {
|
|
25
|
+
request.login(credentials.email, resolve);
|
|
26
|
+
});
|
|
27
|
+
response.send({});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Route } from '@proteinjs/server-api';
|
|
2
|
+
import { routes } from '@proteinjs/user';
|
|
3
|
+
import { destroySession } from '../authentication/destroySession';
|
|
4
|
+
|
|
5
|
+
export const logout: Route = {
|
|
6
|
+
path: routes.logout.path,
|
|
7
|
+
method: routes.logout.method,
|
|
8
|
+
onRequest: async (request: any, response): Promise<void> => {
|
|
9
|
+
// request.logout();
|
|
10
|
+
// await request.session.destroy();
|
|
11
|
+
// await new Promise((resolve, reject) => {
|
|
12
|
+
// request.session?.destroy((error: any) => {
|
|
13
|
+
// if (error) {
|
|
14
|
+
// reject(error);
|
|
15
|
+
// return;
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// resolve();
|
|
19
|
+
// });
|
|
20
|
+
// });
|
|
21
|
+
// delete request._passport?.session?.user;
|
|
22
|
+
// console.info(`request.sessionID: ${request.sessionID}`);
|
|
23
|
+
// console.info(`request.session?.id: ${request.session?.id}`);
|
|
24
|
+
await destroySession(request.sessionID);
|
|
25
|
+
request.session.passport.user = null;
|
|
26
|
+
response.redirect('/login');
|
|
27
|
+
}
|
|
28
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "./",
|
|
4
|
+
"target": "es5",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "./dist/",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noImplicitAny": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"jsx": "react"
|
|
17
|
+
}
|
|
18
|
+
}
|