@olasphe/vanilla 1.0.0

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.
@@ -0,0 +1,9 @@
1
+ import { IncomingMessage, ServerResponse } from 'http';
2
+ import { AuthService } from '@olasubomimk/core';
3
+ export declare class VanillaAuthAdapter {
4
+ private authService;
5
+ constructor(authService: AuthService);
6
+ handle(req: IncomingMessage, res: ServerResponse): Promise<void>;
7
+ private parseBody;
8
+ private sendJson;
9
+ }
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VanillaAuthAdapter = void 0;
4
+ const core_1 = require("@olasubomimk/core");
5
+ const cookie_1 = require("cookie");
6
+ class VanillaAuthAdapter {
7
+ constructor(authService) {
8
+ this.authService = authService;
9
+ }
10
+ async handle(req, res) {
11
+ const url = new URL(req.url || '/', `http://${req.headers.host}`);
12
+ const method = req.method;
13
+ if (url.pathname === '/api/auth/register' && method === 'POST') {
14
+ const body = await this.parseBody(req);
15
+ try {
16
+ const { user } = await this.authService.register(body);
17
+ this.sendJson(res, 201, { message: 'Registered. Please verify email.', userId: user.id });
18
+ }
19
+ catch (e) {
20
+ this.sendJson(res, 400, { error: e.message });
21
+ }
22
+ return;
23
+ }
24
+ if (url.pathname === '/api/auth/login' && method === 'POST') {
25
+ const body = await this.parseBody(req);
26
+ try {
27
+ const { session, user } = await this.authService.login(body.email, body.password);
28
+ // Set Cookie
29
+ const cookieVal = (0, cookie_1.serialize)(core_1.config.AUTH_COOKIE_NAME, session.token, {
30
+ httpOnly: core_1.config.AUTH_COOKIE_HTTPONLY,
31
+ secure: core_1.config.AUTH_COOKIE_SECURE,
32
+ sameSite: core_1.config.AUTH_COOKIE_SAMESITE,
33
+ path: '/',
34
+ maxAge: 60 * 15, // 15 mins default
35
+ });
36
+ res.setHeader('Set-Cookie', cookieVal);
37
+ this.sendJson(res, 200, { message: 'Logged in', user });
38
+ }
39
+ catch (e) {
40
+ this.sendJson(res, 401, { error: e.message });
41
+ }
42
+ return;
43
+ }
44
+ if (url.pathname === '/api/auth/verify' && method === 'POST') {
45
+ const body = await this.parseBody(req);
46
+ try {
47
+ await this.authService.verifyEmail(body.token);
48
+ this.sendJson(res, 200, { message: 'Email verified' });
49
+ }
50
+ catch (e) {
51
+ this.sendJson(res, 400, { error: e.message });
52
+ }
53
+ return;
54
+ }
55
+ // 404
56
+ this.sendJson(res, 404, { error: 'Not Found' });
57
+ }
58
+ parseBody(req) {
59
+ return new Promise((resolve, reject) => {
60
+ let body = '';
61
+ req.on('data', chunk => body += chunk);
62
+ req.on('end', () => {
63
+ try {
64
+ resolve(JSON.parse(body));
65
+ }
66
+ catch (e) {
67
+ reject(e);
68
+ }
69
+ });
70
+ req.on('error', reject);
71
+ });
72
+ }
73
+ sendJson(res, status, data) {
74
+ res.writeHead(status, { 'Content-Type': 'application/json' });
75
+ res.end(JSON.stringify(data));
76
+ }
77
+ }
78
+ exports.VanillaAuthAdapter = VanillaAuthAdapter;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@olasphe/vanilla",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "author": "Auth SDK User <user@example.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/example/auth-sdk-monorepo"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc"
20
+ },
21
+ "dependencies": {
22
+ "@olasphe/core": "*",
23
+ "cookie": "^1.1.1"
24
+ },
25
+ "devDependencies": {
26
+ "@types/cookie": "^0.6.0",
27
+ "@types/node": "^20.0.0"
28
+ }
29
+ }