diff --git a/.gitignore b/.gitignore index efa137e..ab02ea8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ tsconfig.tsbuildinfo node_modules/ data/config/worlds.json +data/players/ # coverage reports coverage/ diff --git a/src/routes/account/index.ts b/src/routes/account/index.ts new file mode 100644 index 0000000..474c4e3 --- /dev/null +++ b/src/routes/account/index.ts @@ -0,0 +1,116 @@ +import bcrypt from 'bcrypt'; +import fs from 'fs'; + +import { db } from '#/db/query.js'; +import { profiles, resolveSelectedProfile } from '#/util/Profile.js'; + +export default function (f: any, opts: any, next: any) { + f.get('/', async (req: any, res: any) => { + // todo: validation handler instead of in route + if (!req.session.account) { + return res.redirect('/account/login', 302); + } + + const profile = resolveSelectedProfile(req); + + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/index', { + success, + error, + profile, + profiles + }); + }); + + f.get('/login', async (req: any, res: any) => { + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/login', { + success, + error + }); + }); + + f.get('/logout', async (req: any, res: any) => { + req.session.account = null; + return res.redirect('/account/login', 302); + }); + + f.get('/forgot', async (req: any, res: any) => { + const { success, error} = req.session; + delete req.session.success; + delete req.session.error; + + return res.view('account/forgot', { + success, + error + }); + }); + + f.post('/login', async (req: any, res: any) => { + const { username, password } = req.body; + + const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirst(); + + if (!account || !(await bcrypt.compare(password.toLowerCase(), account.password))) { + req.session.error = 'Invalid username or password.'; + return res.redirect('/account/login', 302); + } + + req.session.account = { + id: account.id, + username: account.username + }; + + return res.redirect('/account', 302); + }); + + f.post('/password', async (req: any, res: any) => { + if (!req.session.account) { + return res.redirect('/account/login', 302); + } + + const { password, password2 } = req.body; + + if (!password || password.length < 5 || password.length > 20) { + req.session.error = 'Your password must be between 5 and 20 characters long.'; + return res.redirect('/account', 302); + } + + if (password !== password2) { + req.session.error = 'Your passwords do not match.'; + return res.redirect('/account', 302); + } + + const hash = await bcrypt.hash(password.toLowerCase(), 10); + await db.updateTable('account').set({ password: hash }).where('id', '=', req.session.account.id).execute(); + + req.session.success = 'Your password has been updated.'; + return res.redirect('/account', 302); + }); + + f.post('/forgot', async (req: any, res: any) => { + return res.redirect('/account/forgot', 302); + }); + + f.post('/export', async (req: any, res: any) => { + // todo: move to input validation + if (!req.session.account) { + return res.redirect('/account', 302); + } + + const profile = resolveSelectedProfile(req); + if (!fs.existsSync(`data/players/${profile.id}/${req.session.account.username}.sav`)) { + return res.redirect('/account', 302); + } + + return fs.readFileSync(`data/players/${profile.id}/${req.session.account.username}.sav`); + }); + + next(); +} diff --git a/src/routes/hiscores/index.ts b/src/routes/hiscores/index.ts index 38bf317..33c7965 100644 --- a/src/routes/hiscores/index.ts +++ b/src/routes/hiscores/index.ts @@ -1,5 +1,6 @@ import { toDisplayName, toSafeName } from '#/jstring/JString.js'; import { db } from '#/db/query.js'; +import { profiles, resolveSelectedProfile } from '#/util/Profile.js'; const categories = [ { id: 0, name: 'Overall', large: true, level: true }, @@ -24,14 +25,6 @@ const categories = [ { id: 21, name: 'Runecrafting' } ]; -// todo: move to db table -const profiles = [ - { id: 'alpha3', name: 'Alpha 3.0' }, // todo: primary/fallback should be configurable - { id: 'alpha1', name: 'Alpha 1.0' }, - { id: 'alpha2', name: 'Alpha 2.0' }, - { id: 'beta', name: 'Beta' }, -]; - const levelExperience = new Int32Array(99); let acc = 0; @@ -56,20 +49,6 @@ function numberWithCommas(x: number) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } -function resolveSelectedProfile(req: any): { id: string } { - let profile = profiles.find((p) => p.id === req.query.profile); - - if (!profile && req.session.selectedProfile) { - profile = profiles.find((p) => p.id === req.session.selectedProfile); - } - if (!profile) { - profile = profiles[0]; - } - req.session.selectedProfile = profile.id; - - return profile; -} - export default function (f: any, opts: any, next: any) { f.get('/', async (req: any, res: any) => { const profile = resolveSelectedProfile(req); diff --git a/src/routes/website.ts b/src/routes/website.ts index 013240e..d4e881f 100644 --- a/src/routes/website.ts +++ b/src/routes/website.ts @@ -92,4 +92,8 @@ export default async function (app: FastifyInstance) { app.get('/worldmap', async (req, reply) => { return reply.view('worldmap/applet'); }); + + app.get('/guide', async (req, reply) => { + return reply.view('guide'); + }); } diff --git a/src/util/Profile.ts b/src/util/Profile.ts new file mode 100644 index 0000000..21ecaaa --- /dev/null +++ b/src/util/Profile.ts @@ -0,0 +1,21 @@ +// todo: move to db table +export const profiles = [ + { id: 'beta', name: 'Beta' }, + { id: 'alpha1', name: 'Alpha 1.0' }, + { id: 'alpha2', name: 'Alpha 2.0' }, + { id: 'alpha3', name: 'Alpha 3.0' }, +]; + +export function resolveSelectedProfile(req: any): { id: string } { + let profile = profiles.find((p) => p.id === req.query.profile); + + if (!profile && req.session.selectedProfile) { + profile = profiles.find((p) => p.id === req.session.selectedProfile); + } + if (!profile) { + profile = profiles[0]; + } + req.session.selectedProfile = profile.id; + + return profile; +} diff --git a/view/account/forgot.ejs b/view/account/forgot.ejs new file mode 100644 index 0000000..f7155f5 --- /dev/null +++ b/view/account/forgot.ejs @@ -0,0 +1,23 @@ +<%- include('../_partial/header.ejs', { title: 'Forgot Password - Security Centre' }); %> +<%- include('../_components/frame-top.ejs'); %> +<%- include('../_components/content-start.ejs'); %> +<%- include('../_components/title-box.ejs', { title: 'Forgot Password - Security Centre' }); %> + +<%- include('../_components/content-body-start.ejs'); %> + +<% if (success) { %> +
<%= success %>
+<% } else if (error) { %> +<%= error %>
+<% } %> + +
<%= success %>
+<% } else if (error) { %> +<%= error %>
+<% } else { %> +Change your password:
+<% } %> + + + +<%- include('../_components/content-body-end.ejs'); %> +<%= success %>
+<% } else if (error) { %> +<%= error %>
+<% } else { %> +Enter your username & password.
+<% } %> + + +<%- include('../_components/content-body-end.ejs'); %> +Or have you forgotten your password?
+ + +<%- include('../_components/content-body-end.ejs'); %> +This area is under construction - Coming Soon!
+<%- include('./_components/content-body-end.ejs'); %> +
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
