Groundwork

This commit is contained in:
2022-05-08 15:35:43 +03:00
parent 44f6c82a4e
commit 17aa8c75b1
52 changed files with 13631 additions and 29700 deletions

2
backend/src/.env.example Normal file
View File

@@ -0,0 +1,2 @@
PORT=3000
MONGODB_URI=

16
backend/src/getUserId.ts Normal file
View File

@@ -0,0 +1,16 @@
export default async function getUserId(token: string, authProvider: string) {
switch (authProvider) {
case 'yandex':
return getYandexUserId(token);
}
}
async function getYandexUserId(token: string) {
const baseUrl = 'https://login.yandex.ru/info';
const response = await fetch(baseUrl, {
headers: {
Authorization: `OAuth ${token}`
}
})
return (await response.json()).id;
}

16
backend/src/index.ts Normal file
View File

@@ -0,0 +1,16 @@
import { config } from "https://deno.land/x/dotenv@v3.2.0/mod.ts";
import { Application } from "https://deno.land/x/oak@v10.5.1/mod.ts";
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
import routes from './routes.ts'
const app = new Application();
app.use(oakCors({
origin: 'http://localhost:8080',
credentials: true
}));
app.use(routes.routes());
app.use(routes.allowedMethods());
console.log(`👂 on port ${Number(config().PORT)}`);
await app.listen({ port: Number(config().PORT) });

View File

@@ -0,0 +1,21 @@
export default class Task {
name: string;
startedAt?: Date;
running: boolean;
constructor({
name,
startedAt,
totalTime,
category
}: {
name: string,
startedAt?: Date,
totalTime: number,
category: string | null
}) {
this.name = name;
this.startedAt = startedAt;
this.running = false
}
}

View File

@@ -0,0 +1,31 @@
import Task from './Task.ts';
export default class User {
userId: string;
authProvider: string;
tasks: Array<Task>;
categories: Array<string>;
updatedAt: Date;
constructor({
userId,
authProvider
}: {
userId: string,
authProvider: string
}) {
this.userId = userId;
this.authProvider = authProvider;
this.tasks = [];
this.categories = [];
this.updatedAt = new Date();
}
addTask(task: Task) {
this.tasks.push(task);
}
removeTask(taskName: string) {
this.tasks = this.tasks.filter((task) => task.name !== taskName);
}
}

35
backend/src/routes.ts Normal file
View File

@@ -0,0 +1,35 @@
import { config } from "https://deno.land/x/dotenv@v3.2.0/mod.ts";
import { Router } from "https://deno.land/x/oak@v10.5.1/mod.ts";
// import onyx from "https://deno.land/x/onyx@v1.0.1/mod.ts";
import { MongoClient } from "https://deno.land/x/mongo@v0.29.4/mod.ts";
import User from './models/User.ts';
import getUserId from './getUserId.ts';
const client = new MongoClient();
await client.connect(config().MONGODB_URI);
const db = client.database("worktime");
const users = db.collection<User>("users");
const endpoints = new Router()
.post("/login", async (ctx) => {
const body = await ctx.request.body({ type: "json" }).value;
const token = body.token;
const authProvider = body.authProvider;
const userId = await getUserId(token, authProvider);
await users.insertOne(new User({
userId,
authProvider
}));
ctx.response.body = 'success';
})
const routes = new Router()
.use("/api", endpoints.routes(), endpoints.allowedMethods())
// .use(onyx.initialize())
export default routes;