import { Errors } from "../errors" import * as Leaf from "leaf-ts" import { RouteDecorators, APIServiceSpec, RouteService, APIBase, RouteServiceTemplate, ExpressService } from "root-ts" import { AccountDecorators } from "../decorator/account" import { AuthenticationService } from "../service/authenticationService"; import { AccountService } from "../service/accountService"; export class AccountAPIService extends RouteServiceTemplate { readonly name = "api.account" readonly dependencies = this.dependencies.concat(["AccountService", "AuthenticationService", "TTLSessionStorage", "RouteSessionService"]) constructor() { super(AccountAPIServiceSpec) } services: { AuthenticationService: AuthenticationService AccountService: AccountService ExpressService: ExpressService } initialize() { this.api("getTokenStandard", (context) => { if (!context.body.password || !context.body.login) { context.error(new Errors.InvalidParameter) return } let meta = { password: context.body.password } as any let login = context.body.login if (login.indexOf("@") >= 0) { meta.email = login } else { meta.name = login } context.services.AuthenticationService.authenticate({ authentication: { type: "common", meta: meta } }, (err, auth) => { if (err) { context.error(new Errors.AuthorizationFailed("Invalid auth")) return } context.services.AccountService.get(auth.accountId, (err, account) => { if (err) { context.error(new Errors.AuthorizationFailed("Missing account")) return } context.services.AuthenticationService.createToken({ account }, (err, token) => { if (err) { context.error(err) return } context.success({ token, account: account }) }) }) }) }) this.api("webLoginStandard", (context) => { if (!context.body.password || !context.body.login) { context.error(new Errors.InvalidParameter) return } let meta = { password: context.body.password } as any let login = context.body.login if (login.indexOf("@") >= 0) { meta.email = login } else { meta.name = login } context.services.AuthenticationService.authenticate({ authentication: { type: "common", meta: meta } }, (err, auth) => { if (err) { context.error(new Errors.AuthorizationFailed) return } context.services.AccountService.get(auth.accountId, (err, account) => { if (err) { context.error(new Errors.AuthorizationFailed("Missing account")) return } let session = context.req["session"] || {} session["account"] = account context.success({ account: account }) }) }) }) this.api("registerStandard", (context) => { this.services.AccountService.register({ type: "common", meta: { active: true, displayName: context.body.name, password: context.body.password, email: context.body.email, name: context.body.name, } }, (err, account) => { if (err) { context.error(err) return } let session = context.req["session"] || {} session["account"] = account context.success({ account: account }) }) }) this.decorate(AccountDecorators.decorate("WithAccount")).api("getCurrentAccount", (context) => { context.success(context.info.account) }) this.installTo(this.services.ExpressService.server) } } export namespace AccountAPIServiceSpec { export const prefix = "/api/" export const name = "api.account" export const API = { getCurrentAccount: new class extends APIBase { readonly method = "POST" readonly path = "account/me/GET" info: { account: Type.Account.AccountService.Account } return: Type.Account.AccountService.Account }, webLoginStandard: new class extends APIBase { readonly method = "POST" readonly path = "account/authentication/standard/GET" body: { // email or accountname login: string password: string } return: { account: Type.Account.AccountService.Account } }, getTokenStandard: new class extends APIBase { readonly method = "POST" readonly path = "account/authentication/standard/token/GET" body: { // email or accountname login: string password: string } return: { account: Type.Account.AccountService.Account token: string } }, // Creating a account will also create default workspace for him registerStandard: new class extends APIBase { readonly method = "POST" readonly path = "account/authentication/standard" body: { name: string password: string email: string } return: { account: Type.Account.AccountService.Account } } } }