diff options
author | Joseph Ditton <jditton.atomic@gmail.com> | 2021-11-22 14:21:53 -0700 |
---|---|---|
committer | Joseph Ditton <jditton.atomic@gmail.com> | 2021-11-22 14:21:53 -0700 |
commit | 4ae4e874689a71e33cdd7a5799fc0c85c4861367 (patch) | |
tree | d60c5d5f05ce0d0574bc168084e2b014ee999c1b | |
parent | 3902da1747a3e32db0b67f1162eafd4860b3d27a (diff) | |
download | locchat-4ae4e874689a71e33cdd7a5799fc0c85c4861367.tar.gz locchat-4ae4e874689a71e33cdd7a5799fc0c85c4861367.zip |
adds start for console
-rw-r--r-- | client/components/home/_home.jsx | 22 | ||||
-rw-r--r-- | client/components/router.jsx | 4 | ||||
-rw-r--r-- | client/components/sign_in/_sign_in.jsx | 28 | ||||
-rw-r--r-- | client/components/sign_up/_sign_up.jsx | 35 | ||||
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | server/app.controller.ts | 8 | ||||
-rw-r--r-- | server/console.ts | 39 | ||||
-rw-r--r-- | server/controllers/sessions.controller.ts | 14 | ||||
-rw-r--r-- | server/dto/sign_in.dto.ts | 2 | ||||
-rw-r--r-- | server/providers/services/users.service.ts | 2 | ||||
-rw-r--r-- | views/index.hbs | 4 | ||||
-rw-r--r-- | yarn.lock | 110 |
12 files changed, 240 insertions, 32 deletions
diff --git a/client/components/home/_home.jsx b/client/components/home/_home.jsx index 59389ad..02ebe38 100644 --- a/client/components/home/_home.jsx +++ b/client/components/home/_home.jsx @@ -1,3 +1,23 @@ +import { useContext } from 'react'; +import { SettingsContext } from '../../utils/settings_context'; + export const Home = () => { - return <div>I am the home page</div>; + const [, dispatch] = useContext(SettingsContext); + const logout = async () => { + const res = await fetch('/sessions', { + method: 'DELETE', + }); + if (res.status === 200) { + dispatch({ type: 'update', payload: { jwt: undefined } }); + } + }; + + return ( + <div> + <h1>Welcome</h1> + <button type="button" onClick={logout}> + Logout + </button> + </div> + ); }; diff --git a/client/components/router.jsx b/client/components/router.jsx index 6760bea..59ea7c3 100644 --- a/client/components/router.jsx +++ b/client/components/router.jsx @@ -7,12 +7,12 @@ import { SignUp } from './sign_up/_sign_up'; export const Router = () => { const [settings] = useContext(SettingsContext); - const { JWT } = settings; + const { jwt } = settings; return ( <Routes> <Route path="/" - element={JWT ? <Home /> : <Navigate replace to="signin" />} // no JWT means not logged in + element={jwt ? <Home /> : <Navigate replace to="signin" />} // no jwt means not logged in /> <Route path="signin" element={<SignIn />} /> <Route path="signup" element={<SignUp />} /> diff --git a/client/components/sign_in/_sign_in.jsx b/client/components/sign_in/_sign_in.jsx index 753b2b5..a6e802c 100644 --- a/client/components/sign_in/_sign_in.jsx +++ b/client/components/sign_in/_sign_in.jsx @@ -1,7 +1,9 @@ -import { useState } from 'react'; +import { useContext, useState } from 'react'; import { useNavigate } from 'react-router'; +import { SettingsContext } from '../../utils/settings_context'; export const SignIn = () => { + const [, dispatch] = useContext(SettingsContext); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const navigate = useNavigate(); @@ -10,6 +12,26 @@ export const SignIn = () => { navigate('/signup'); }; + const signIn = async () => { + const res = await fetch('/sessions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + email, + password, + }), + }); + if (res.status === 201) { + const result = await res.json(); + dispatch({ type: 'update', payload: { jwt: result.token } }); + navigate('/'); + } else { + console.error('An issue occurred when logging in.'); + } + }; + return ( <div> <div>Email</div> @@ -25,7 +47,9 @@ export const SignIn = () => { onChange={(e) => setPassword(e.target.value)} /> <div> - <button type="button">Sign in</button> + <button type="button" onClick={signIn}> + Sign in + </button> </div> <div> <button type="button" onClick={goToSignUp}> diff --git a/client/components/sign_up/_sign_up.jsx b/client/components/sign_up/_sign_up.jsx index bbbd51b..13ac6c7 100644 --- a/client/components/sign_up/_sign_up.jsx +++ b/client/components/sign_up/_sign_up.jsx @@ -1,6 +1,10 @@ -import { useState } from 'react'; +import { useContext, useState } from 'react'; +import { useNavigate } from 'react-router'; +import { SettingsContext } from '../../utils/settings_context'; export const SignUp = () => { + const [, dispatch] = useContext(SettingsContext); + const navigate = useNavigate(); const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [emailConfirmation, setEmailConfirmation] = useState(''); @@ -29,20 +33,21 @@ export const SignUp = () => { setErrorMessage('Name cannot be blank.'); return; } - try { - await fetch('/users', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - name, - email, - password, - }), - }); - } catch (e) { - console.log(e.message); + const res = await fetch('/users', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + name, + email, + password, + }), + }); + if (res.status === 201) { + const result = await res.json(); + dispatch({ type: 'update', payload: { jwt: result.token } }); + navigate('/'); } }; diff --git a/package.json b/package.json index 3bdf462..0b8399c 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "prebuild": "rimraf dist", "build": "nest build && yarn client:build", "format": "prettier --write \"server/**/*.ts\" \"test/**/*.ts\"", + "console": "ts-node -r tsconfig-paths/register server/console.ts", "start": "nest start", "start:dev": "yarn db:start && nest start --watch", "start:debug": "yarn db:start && nest start --debug --watch", @@ -56,12 +57,15 @@ "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^4.28.2", "@typescript-eslint/parser": "^4.28.2", + "await-outside": "^3.0.0", "eslint": "^7.30.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", "jest": "^27.0.6", "parcel": "^2.0.1", "prettier": "^2.4.1", + "purdy": "^3.5.1", + "repl": "^0.1.3", "supertest": "^6.1.3", "ts-jest": "^27.0.3", "ts-node": "^10.0.0", diff --git a/server/app.controller.ts b/server/app.controller.ts index a6bcf58..685cf8f 100644 --- a/server/app.controller.ts +++ b/server/app.controller.ts @@ -1,8 +1,12 @@ -import { Controller, Get, Render } from '@nestjs/common'; +import { Controller, Get, Render, Req } from '@nestjs/common'; +import { Request } from 'express'; @Controller() export class AppController { @Get() @Render('index') - index() {} + index(@Req() req: Request) { + const jwt = req.cookies['_token']; + return { jwt }; + } } diff --git a/server/console.ts b/server/console.ts new file mode 100644 index 0000000..485789a --- /dev/null +++ b/server/console.ts @@ -0,0 +1,39 @@ +import 'dotenv/config'; +import { NestFactory } from '@nestjs/core'; +import * as repl from 'repl'; +import * as Logger from 'purdy'; + +const LOGGER_OPTIONS = { + indent: 2, + depth: 1, +}; + +class InteractiveNestJS { + async run() { + // create the application context + // eslint-disable-next-line @typescript-eslint/no-var-requires + const targetModule = require(`${__dirname}/app.module`); + const applicationContext = await NestFactory.createApplicationContext( + // tslint:disable-next-line: no-string-literal + targetModule['AppModule'], + ); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const awaitOutside = require('await-outside'); + // start node repl + const server = repl.start({ + useColors: true, + prompt: '> ', + writer: replWriter, + ignoreUndefined: true, + }); + server.context.app = applicationContext; + awaitOutside.addAwaitOutsideToReplServer(server); + } +} + +function replWriter(value: any): string { + return Logger.stringify(value, LOGGER_OPTIONS); +} + +const session = new InteractiveNestJS(); +session.run(); diff --git a/server/controllers/sessions.controller.ts b/server/controllers/sessions.controller.ts index 3b179ad..90b8e78 100644 --- a/server/controllers/sessions.controller.ts +++ b/server/controllers/sessions.controller.ts @@ -1,9 +1,11 @@ import { Body, Controller, + Delete, HttpException, HttpStatus, Post, + Redirect, Res, } from '@nestjs/common'; import { Response } from 'express'; @@ -18,13 +20,13 @@ import { SignInDto } from 'server/dto/sign_in.dto'; export class SessionsController { constructor(private usersService: UsersService) {} - @Post('/sign_in') - async signIn( + @Post('/sessions') + async create( @Body() body: SignInDto, @Res({ passthrough: true }) res: Response, ) { const { verified, user } = await this.usersService.verify( - body.username, + body.email, body.password, ); @@ -45,4 +47,10 @@ export class SessionsController { res.cookie('_token', token); return { token }; } + + @Delete('/sessions') + async destroy(@Res({ passthrough: true }) res: Response) { + res.clearCookie('_token'); + return { success: true }; + } } diff --git a/server/dto/sign_in.dto.ts b/server/dto/sign_in.dto.ts index f480c43..0671602 100644 --- a/server/dto/sign_in.dto.ts +++ b/server/dto/sign_in.dto.ts @@ -1,4 +1,4 @@ export class SignInDto { - username: string; + email: string; password: string; } diff --git a/server/providers/services/users.service.ts b/server/providers/services/users.service.ts index 0f502b2..21438a4 100644 --- a/server/providers/services/users.service.ts +++ b/server/providers/services/users.service.ts @@ -30,6 +30,6 @@ export class UsersService { password, user.password_hash, ); - return { verified, user }; + return { verified, user: verified ? user : null }; } } diff --git a/views/index.hbs b/views/index.hbs index fed0488..30a3987 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,7 +1,9 @@ <html> <head> <script type="text/javascript"> - window.SETTINGS = {}; + window.SETTINGS = { + jwt: '{{jwt}}' + }; </script> </head> <body> @@ -2195,6 +2195,14 @@ async-limiter@~1.0.0: resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd" integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ== +async-to-gen@~1.3.2: + version "1.3.3" + resolved "https://registry.yarnpkg.com/async-to-gen/-/async-to-gen-1.3.3.tgz#d52c9fb4801f0df44abc4d2de1870b48b60e20bb" + integrity sha1-1SyftIAfDfRKvE0t4YcLSLYOILs= + dependencies: + babylon "^6.14.0" + magic-string "^0.19.0" + async@0.9.x: version "0.9.2" resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d" @@ -2215,6 +2223,13 @@ available-typed-arrays@^1.0.5: resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7" integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw== +await-outside@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/await-outside/-/await-outside-3.0.0.tgz#cf97dff66dbb6fe635935e541f40d676464f0a6d" + integrity sha512-ZIxAOB+YJvmzUR4VKVX+SWtdlXfNUAv0fkZWZvtz12A5lexesibQDeiK+XhPanA6DMCscuT+9tZ3OBIPGTmZ7A== + dependencies: + async-to-gen "~1.3.2" + aws-sign2@~0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" @@ -2293,6 +2308,11 @@ babel-preset-jest@^27.2.0: babel-plugin-jest-hoist "^27.2.0" babel-preset-current-node-syntax "^1.0.0" +babylon@^6.14.0: + version "6.18.0" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" + integrity sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ== + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -2375,6 +2395,14 @@ boolbase@^1.0.0: resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e" integrity sha1-aN/1++YMUes3cl6p4+0xDcwed24= +bossy@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/bossy/-/bossy-3.0.4.tgz#f9ae9f26e81b41a318f4ee0d83686e4a5c2507b9" + integrity sha1-+a6fJugbQaMY9O4Ng2huSlwlB7k= + dependencies: + hoek "4.x.x" + joi "10.x.x" + brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" @@ -4426,6 +4454,11 @@ hmac-drbg@^1.0.1: minimalistic-assert "^1.0.0" minimalistic-crypto-utils "^1.0.1" +hoek@4.x.x: + version "4.2.1" + resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb" + integrity sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA== + html-encoding-sniffer@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/html-encoding-sniffer/-/html-encoding-sniffer-1.0.2.tgz#e70d84b94da53aa375e11fe3a351be6642ca46f8" @@ -4933,6 +4966,18 @@ isarray@~1.0.0: resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= +isemail@2.x.x: + version "2.2.1" + resolved "https://registry.yarnpkg.com/isemail/-/isemail-2.2.1.tgz#0353d3d9a62951080c262c2aa0a42b8ea8e9e2a6" + integrity sha1-A1PT2aYpUQgMJiwqoKQrjqjp4qY= + +isemail@3.x.x: + version "3.2.0" + resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.2.0.tgz#59310a021931a9fb06bbb51e155ce0b3f236832c" + integrity sha512-zKqkK+O+dGqevc93KNsbZ/TqTUFd46MwWjYOoMrjIMZ51eU7DtQG3Wmd9SQQT7i7RVnuTPEiYEWHU3MSbxC1Tg== + dependencies: + punycode "2.x.x" + isexe@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" @@ -4995,6 +5040,11 @@ istanbul-reports@^3.0.2: html-escaper "^2.0.0" istanbul-lib-report "^3.0.0" +items@2.x.x: + version "2.1.2" + resolved "https://registry.yarnpkg.com/items/-/items-2.1.2.tgz#0849354595805d586dac98e7e6e85556ea838558" + integrity sha512-kezcEqgB97BGeZZYtX/MA8AG410ptURstvnz5RAgyFZ8wQFPMxHY8GpTq+/ZHKT3frSlIthUq7EvLt9xn3TvXg== + iterare@1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/iterare/-/iterare-1.2.1.tgz#139c400ff7363690e33abffa33cbba8920f00042" @@ -5420,6 +5470,25 @@ jest@^27.0.6: import-local "^3.0.2" jest-cli "^27.3.1" +joi@10.x.x: + version "10.6.0" + resolved "https://registry.yarnpkg.com/joi/-/joi-10.6.0.tgz#52587f02d52b8b75cdb0c74f0b164a191a0e1fc2" + integrity sha512-hBF3LcqyAid+9X/pwg+eXjD2QBZI5eXnBFJYaAkH4SK3mp9QSRiiQnDYlmlz5pccMvnLcJRS4whhDOTCkmsAdQ== + dependencies: + hoek "4.x.x" + isemail "2.x.x" + items "2.x.x" + topo "2.x.x" + +joi@^12.0.0: + version "12.0.0" + resolved "https://registry.yarnpkg.com/joi/-/joi-12.0.0.tgz#46f55e68f4d9628f01bbb695902c8b307ad8d33a" + integrity sha512-z0FNlV4NGgjQN1fdtHYXf5kmgludM65fG/JlXzU6+rwkt9U5UWuXVYnXa2FpK0u6+qBuCmrm5byPNuiiddAHvQ== + dependencies: + hoek "4.x.x" + isemail "3.x.x" + topo "2.x.x" + js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -5810,6 +5879,13 @@ magic-string@0.25.7: dependencies: sourcemap-codec "^1.4.4" +magic-string@^0.19.0: + version "0.19.1" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.19.1.tgz#14d768013caf2ec8fdea16a49af82fc377e75201" + integrity sha1-FNdoATyvLsj96hakmvgvw3fnUgE= + dependencies: + vlq "^0.2.1" + make-dir@^3.0.0, make-dir@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f" @@ -7084,15 +7160,24 @@ punycode@1.3.2: resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d" integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0= +punycode@2.x.x, punycode@^2.1.0, punycode@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" + integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== + punycode@^1.3.2, punycode@^1.4.1: version "1.4.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" integrity sha1-wNWmOycYgArY4esPpSachN1BhF4= -punycode@^2.1.0, punycode@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" - integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== +purdy@^3.5.1: + version "3.5.1" + resolved "https://registry.yarnpkg.com/purdy/-/purdy-3.5.1.tgz#addb87ca765d7b4a04c7b250c8c6f504d1827a2e" + integrity sha512-2v+aHv71zgOES8odUqnlbxLq+p2TeM51CtKLaUKuedsKWI4atGHO7IlQyUJuVCUJ7gECJ936ugOiA3gtAxSNzQ== + dependencies: + bossy "^3.0.4" + chalk "^2.4.1" + joi "^12.0.0" purgecss@^4.0.0: version "4.0.3" @@ -7247,6 +7332,11 @@ relateurl@^0.2.7: resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9" integrity sha1-VNvzd+UUQKypCkzSdGANP/LYiKk= +repl@^0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/repl/-/repl-0.1.3.tgz#2f05d42b0c88b43d05ccbda10ed14aeff5699b60" + integrity sha1-LwXUKwyItD0FzL2hDtFK7/Vpm2A= + request-promise-core@1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.4.tgz#3eedd4223208d419867b78ce815167d10593a22f" @@ -8050,6 +8140,13 @@ toidentifier@1.0.0: resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw== +topo@2.x.x: + version "2.0.2" + resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182" + integrity sha1-zVYVdSU5BXwNwEkaYhw7xvvh0YI= + dependencies: + hoek "4.x.x" + tough-cookie@^2.3.3, tough-cookie@^2.5.0, tough-cookie@~2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2" @@ -8393,6 +8490,11 @@ verror@1.10.0: core-util-is "1.0.2" extsprintf "^1.2.0" +vlq@^0.2.1: + version "0.2.3" + resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26" + integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow== + vm-browserify@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0" |