Skip to content

Commit

Permalink
Authentication Enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
StraightUpCode committed May 28, 2020
1 parent 1f19d42 commit 307ed26
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application } from "./deps.ts";
import { TodoRouter, MainRouter } from "./routes/mod.ts";
import { TodoRouter, MainRouter, AuthRouter } from "./routes/mod.ts";
const server = new Application();

//Routes
Expand All @@ -10,6 +10,7 @@ server.use((ctx, next) => {

server.use(MainRouter.routes());
server.use(TodoRouter.routes());
server.use(AuthRouter.routes());

//server.use(MainRouter.allowedMethods())

Expand Down
19 changes: 17 additions & 2 deletions models/User.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { DATA_TYPES, Model } from "../deps.ts";

interface LoginResult {
export interface LoginResult {
matchingResult: boolean;
result?: any;
}

export interface UserModel {
id: number;
username: string;
password: string;
email: string;
created_at?: Date;
updated_at?: Date;
}

export interface UserCredential {
username: string;
password: string;
}

class User extends Model {
static table = "user";
static fields = {
Expand All @@ -17,7 +31,8 @@ class User extends Model {
email: DATA_TYPES.STRING,
};

static async logIn(username: string, password: string): Promise<LoginResult> {
static async logIn(UserCredential: UserCredential): Promise<LoginResult> {
const { username, password } = UserCredential;
const user = await User.where({
username,
password,
Expand Down
2 changes: 1 addition & 1 deletion models/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ db.link([TodoModel]);
db.link([TodoStatus]);
db.link([User]);

await db.sync({ drop: true });
//await db.sync();
console.log("Awaiting Db Sync");

export default db;
Expand Down
28 changes: 24 additions & 4 deletions routes/auth_router.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { Router } from "../deps.ts";
import User, { LoginResult, UserCredential } from "../models/User.ts";

const AuthRouter = new Router({
prefix: "/",
const AuthRouter = new Router();

AuthRouter.post("/login", async (ctx) => {
const userCredentials: UserCredential = (await ctx.request.body()).value;
console.log("Login");
const user: LoginResult = await User.logIn(userCredentials);
console.log(user);
});

AuthRouter.get("/login", async (ctx) => {
ctx.response.body = "Hello world";
AuthRouter.post("/register", async (ctx) => {
console.log("register");
const newUser = (await ctx.request.body()).value;
console.log(newUser);
const result = await User.create(newUser);
if (result) {
ctx.response.status = 200;
ctx.response.body = {
"message": "User created Succesfully",
};
}
});

AuthRouter.get('/logout', async(ctx) => {

console.log('Log out')
})

export default AuthRouter;

0 comments on commit 307ed26

Please sign in to comment.