Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
fix: fixed bugs with auth and users
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 4, 2020
1 parent 4ed724f commit 225794c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
20 changes: 14 additions & 6 deletions packages/api/knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ config();

const options = {
client: process.env.DB_CLIENT,
connection: process.env.CONNECTION || {
filename: "db/db.sqlite3"
},
connection: process.env.CONNECTION
? {
database: process.env.CONNECTION
}
: {
filename: "db/db.sqlite3"
},
migrations: {
directory: "db/migrations",
tableName: "migrations"
Expand All @@ -25,9 +29,13 @@ const configs: Record<string, Config> = {

test: {
...options,
connection: process.env.TEST_CONNECTION || {
filename: "db/testdb.sqlite3"
}
connection: process.env.TEST_CONNECTION
? {
database: process.env.TEST_CONNECTION
}
: {
filename: "db/db.sqlite3"
}
},

production: {
Expand Down
16 changes: 8 additions & 8 deletions packages/api/src/modules/auth/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import users from "../../../db/seeds/examples/users";
const agent = request.agent(server);

describe("Auth router", () => {
before(async function() {
this.timeout(5000);
await knex.migrate.latest();
await knex.seed.run();
});

it("Logs-in a user", async () => {
const { email, name, password } = users[0];

Expand All @@ -28,17 +34,11 @@ describe("Auth router", () => {
});

it("Logs-out a user", async () => {
const { email, password } = users[0];

const response = await agent
.get(`/api/auth/logout`)
.send({ email, password })
.set("Accept", "application/json")
.set("Accept", "application/text")
.expect(200);

expect(response.body).to.deep.equal({
status: 200,
message: "Successfully logged out"
});
expect(response.text).to.equal("Successfully logged out");
});
});
6 changes: 3 additions & 3 deletions packages/api/src/modules/auth/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router.post(
if (!user)
throw new HttpError(
400,
"There seems to be nco user with that email"
"There seems to be no user with that email"
);

const valid = await bcrypt.compare(password, user.password);
Expand All @@ -34,14 +34,14 @@ router.post(
ctx.body = {
status: 200,
message: "Successfully log in",
user: { username: user.username, role: user.role }
user: { name: user.name, role: user.role }
};

await next();
}
);

router.post("/logout", requireAuthenticated(), async (ctx, next) => {
router.get("/logout", requireAuthenticated(), async (ctx, next) => {
ctx.session = null;

ctx.status = 200;
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/users/actions/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import bcrypt from "bcrypt";
import User from "../types/User";

export const createUser = async (
user: Pick<User, "email" | "username" | "password">
user: Pick<User, "email" | "name" | "password">
) => {
const isTaken = Boolean(
await knex<User>("users")
.select("id")
.where({ username: user.username })
.where({ name: user.name })
.orWhere({ email: user.email })
.first()
);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/modules/users/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const agent = request.agent(server);

const newUser = {
email: "[email protected]",
username: "SomeGuy",
name: "SomeGuy",
password: "WhatShouldITypeHere88@"
};

Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/users/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ router.post(
"/createUser",
validateSchema(registerBody, "body"),
async (ctx, next) => {
const { username, password, email } = ctx.request.body as RegisterBody;
const { name, password, email } = ctx.request.body as RegisterBody;

const user = await createUser({ email, username, password });
const user = await createUser({ email, name, password });

if (!user) {
throw new HttpError(400, "That username seems to be already taken");
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/modules/users/types/RegisterBody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface RegisterBody {
email: string;
username: string;
name: string;
password: string;
}
2 changes: 1 addition & 1 deletion packages/api/src/modules/users/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default interface User {
id: number;
email: string;
username: string;
name: string;
password: string;
role?: string | null;
description?: string | null;
Expand Down

0 comments on commit 225794c

Please sign in to comment.