Sync method

This commit is contained in:
Анатолий Копыл
2022-05-09 03:02:19 +03:00
parent b741964c3a
commit 7458750053
10 changed files with 126 additions and 21 deletions

View File

@@ -0,0 +1,7 @@
export default async (ctx: any, next: Function) => {
const userId = await ctx.state.session.get('userId');
if (!userId) {
ctx.throw(403);
}
await next();
}

View File

@@ -3,7 +3,7 @@ import { Application } from "https://deno.land/x/oak@v10.5.1/mod.ts";
import { Session, CookieStore } from "https://deno.land/x/oak_sessions@v3.4.0/mod.ts";
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
import routes from './routes/index.ts'
import routes from './routes.ts'
const PORT = Number(Deno.env.get('PORT'));
const SECRET = Deno.env.get('SECRET');

View File

@@ -0,0 +1,9 @@
export default class Error {
msg: string;
code: string;
constructor(msg: string, type: string, code: number) {
this.msg = msg;
this.code = `${type}-${code}`;
}
}

View File

@@ -10,17 +10,29 @@ export default class User {
constructor({
providerId,
authProvider
authProvider,
tasks,
categories,
updatedAt
}: {
providerId: string,
authProvider: string
authProvider: string,
tasks?: Array<Task>,
categories?: Array<string>,
updatedAt?: Date | string
}) {
this.providerId = providerId;
this.authProvider = authProvider;
this.id = `${providerId}@${authProvider}`;
this.tasks = [];
this.categories = [];
this.updatedAt = new Date();
this.tasks = tasks ?? [];
this.categories = categories ?? [];
if (updatedAt instanceof Date) {
this.updatedAt = updatedAt;
} else if (updatedAt) {
this.updatedAt = new Date(updatedAt);
} else {
this.updatedAt = new Date();
}
}
addTask(task: Task) {

View File

@@ -2,8 +2,9 @@ import "https://deno.land/x/dotenv@v3.2.0/load.ts";
import { Router } from "https://deno.land/x/oak@v10.5.1/mod.ts";
import { MongoClient } from "https://deno.land/x/mongo@v0.29.4/mod.ts";
import User from '../models/User.ts';
import getProviderId from '../getProviderId.ts';
import User from './models/User.ts';
import authorized from './authorized.ts';
import getProviderId from './getProviderId.ts';
const MONGODB_URI = String(Deno.env.get('MONGODB_URI'));
@@ -16,10 +17,21 @@ const users = db.collection<User>("users");
const endpoints = new Router()
.post("/login", async (ctx) => {
const body = await ctx.request.body({ type: "json" }).value;
const userId = await ctx.state.session.get('userId')
const token = body.token;
const authProvider = body.authProvider;
if (userId) {
ctx.response.body = 'success';
return
}
const providerId = await getProviderId(token, authProvider);
if (!providerId) {
ctx.response.status = 500
ctx.response.body = 'error';
return
}
let user = await users.findOne({
providerId,
@@ -38,6 +50,26 @@ const endpoints = new Router()
ctx.response.body = 'success';
})
.post('/sync', authorized, async (ctx) => {
const body = await ctx.request.body({ type: "json" }).value;
const userId = await ctx.state.session.get('userId')
const clientSideUser = new User(body.user);
const serverSideUser = await users.findOne({ id: userId })
if (!serverSideUser) {
ctx.response.status = 500
ctx.response.body = 'error';
return
}
if (serverSideUser.updatedAt <= clientSideUser.updatedAt) {
serverSideUser.tasks = clientSideUser.tasks;
serverSideUser.categories = clientSideUser.categories;
serverSideUser.updatedAt = new Date();
}
ctx.response.body = serverSideUser;
})
const routes = new Router()
.use("/api", endpoints.routes(), endpoints.allowedMethods())