Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion public/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const operations = [
endpoint: "/api/logout",
method: "POST",
fields: {},
}
},
{
name: "Register",
endpoint: "/api/register",
method: "POST",
fields: { username: "input" },
},
];

const API_URL = "http://localhost:3000";
Expand Down
4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import UserConcept from "./concepts/user";
import WebSessionConcept from "./concepts/websession";

// App Definition using concepts. Right now, we only have our WebSession concept implemented.
export const WebSession = new WebSessionConcept();
export const WebSession = new WebSessionConcept();
export const User = new UserConcept();
23 changes: 23 additions & 0 deletions src/concepts/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NotAllowedError } from "./errors";

export interface UserDoc {
username: string;
}

export default class UserConcept {
// Hardcoded users just for recitation purposes
public readonly users: UserDoc[] = [{ username: "testAccount" }, { username: "grace" }];

register(username: string) {
this.isUsernameAvailable(username);
this.users.push({ username });
}

isUsernameAvailable(username: string) {
if (this.users.filter((user) => user.username === username).length !== 0) {
throw new NotAllowedError("Username already taken!");
}
}

// TODO: Write code to make sure the username exists
}
4 changes: 3 additions & 1 deletion src/concepts/websession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is (some of) the code for the WebSession concept which was introduced in lecture on 9/18.
// We're storing the user (in the form of the username string for now) when the user logs in, and we
// We're storing the user (in the form of the username string for now) when the user logs in, and we
// reset the session's user when the user logs out.

import { SessionData } from "express-session";
Expand Down Expand Up @@ -28,6 +28,8 @@ export default class WebSessionConcept {
// Hint: Take a look at how the "end" function makes sure the user is logged in. Keep in mind that a
// synchronization like starting a session should just consist of a series of actions that may throw
// exceptions and should not have its own control flow.

// TODO: make sure user is registered before trying to log the user in
session.user = username;
}

Expand Down
8 changes: 7 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router, getExpressRouter } from "./framework/router";

import { WebSession } from "./app";
import { User, WebSession } from "./app";
import { WebSessionDoc } from "./concepts/websession";

class Routes {
Expand All @@ -16,6 +16,12 @@ class Routes {
return { msg: "Logged out!" };
}

@Router.post("/register")
async register(username: string) {
User.register(username);
return { msg: "Username registered!", user: username };
}

// If we had more concepts working, we'd add routes for those here too!
}

Expand Down