mirror of
https://github.com/docmost/docmost.git
synced 2025-11-15 01:21:12 +10:00
Return user and with current workspace
* Create endpoint to return user and their workspace * Only return auth tokens on login/signup * Allow nullable workspace name
This commit is contained in:
4
server/src/core/auth/dto/tokens.dto.ts
Normal file
4
server/src/core/auth/dto/tokens.dto.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export interface TokensDto {
|
||||||
|
accessToken: string;
|
||||||
|
refreshToken: string;
|
||||||
|
}
|
||||||
@ -1,13 +1,10 @@
|
|||||||
import {
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||||
BadRequestException,
|
|
||||||
Injectable,
|
|
||||||
UnauthorizedException,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { LoginDto } from '../dto/login.dto';
|
import { LoginDto } from '../dto/login.dto';
|
||||||
import { User } from '../../user/entities/user.entity';
|
import { User } from '../../user/entities/user.entity';
|
||||||
import { CreateUserDto } from '../../user/dto/create-user.dto';
|
import { CreateUserDto } from '../../user/dto/create-user.dto';
|
||||||
import { UserService } from '../../user/user.service';
|
import { UserService } from '../../user/user.service';
|
||||||
import { TokenService } from './token.service';
|
import { TokenService } from './token.service';
|
||||||
|
import { TokensDto } from '../dto/tokens.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
@ -29,16 +26,16 @@ export class AuthService {
|
|||||||
|
|
||||||
user.lastLoginAt = new Date();
|
user.lastLoginAt = new Date();
|
||||||
|
|
||||||
const token: string = await this.tokenService.generateJwt(user);
|
const tokens: TokensDto = await this.tokenService.generateTokens(user);
|
||||||
|
|
||||||
return { user, token };
|
return { tokens };
|
||||||
}
|
}
|
||||||
|
|
||||||
async register(createUserDto: CreateUserDto) {
|
async register(createUserDto: CreateUserDto) {
|
||||||
const user: User = await this.userService.create(createUserDto);
|
const user: User = await this.userService.create(createUserDto);
|
||||||
|
|
||||||
const token: string = await this.tokenService.generateJwt(user);
|
const tokens: TokensDto = await this.tokenService.generateTokens(user);
|
||||||
|
|
||||||
return { user, token };
|
return { tokens };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { JwtService } from '@nestjs/jwt';
|
|||||||
import { EnvironmentService } from '../../../environment/environment.service';
|
import { EnvironmentService } from '../../../environment/environment.service';
|
||||||
import { User } from '../../user/entities/user.entity';
|
import { User } from '../../user/entities/user.entity';
|
||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
|
import { TokensDto } from "../dto/tokens.dto";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TokenService {
|
export class TokenService {
|
||||||
@ -18,6 +19,13 @@ export class TokenService {
|
|||||||
return await this.jwtService.signAsync(payload);
|
return await this.jwtService.signAsync(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async generateTokens(user: User): Promise<TokensDto> {
|
||||||
|
return {
|
||||||
|
accessToken: await this.generateJwt(user),
|
||||||
|
refreshToken: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async verifyJwt(token: string) {
|
async verifyJwt(token: string) {
|
||||||
return await this.jwtService.verifyAsync(token, {
|
return await this.jwtService.verifyAsync(token, {
|
||||||
secret: this.environmentService.getJwtSecret(),
|
secret: this.environmentService.getJwtSecret(),
|
||||||
|
|||||||
@ -1,11 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
Post,
|
|
||||||
Body,
|
|
||||||
Patch,
|
|
||||||
Param,
|
|
||||||
Delete,
|
|
||||||
UseGuards,
|
UseGuards,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
@ -13,11 +8,10 @@ import {
|
|||||||
UnauthorizedException,
|
UnauthorizedException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
import { CreateUserDto } from './dto/create-user.dto';
|
|
||||||
import { UpdateUserDto } from './dto/update-user.dto';
|
|
||||||
import { JwtGuard } from '../auth/guards/JwtGuard';
|
import { JwtGuard } from '../auth/guards/JwtGuard';
|
||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
import { User } from './entities/user.entity';
|
import { User } from './entities/user.entity';
|
||||||
|
import { Workspace } from '../workspace/entities/workspace.entity';
|
||||||
|
|
||||||
@UseGuards(JwtGuard)
|
@UseGuards(JwtGuard)
|
||||||
@Controller('user')
|
@Controller('user')
|
||||||
@ -36,4 +30,15 @@ export class UserController {
|
|||||||
|
|
||||||
return { user };
|
return { user };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
|
@Get('info')
|
||||||
|
async getUserInfo(@Req() req: FastifyRequest) {
|
||||||
|
const jwtPayload = req['user'];
|
||||||
|
|
||||||
|
const data: { workspace: Workspace; user: User } =
|
||||||
|
await this.userService.getUserInstance(jwtPayload.sub);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import { plainToInstance } from 'class-transformer';
|
|||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { WorkspaceService } from '../workspace/services/workspace.service';
|
import { WorkspaceService } from '../workspace/services/workspace.service';
|
||||||
import { CreateWorkspaceDto } from '../workspace/dto/create-workspace.dto';
|
import { CreateWorkspaceDto } from '../workspace/dto/create-workspace.dto';
|
||||||
|
import { Workspace } from "../workspace/entities/workspace.entity";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserService {
|
export class UserService {
|
||||||
@ -27,17 +28,20 @@ export class UserService {
|
|||||||
|
|
||||||
user = await this.userRepository.save(user);
|
user = await this.userRepository.save(user);
|
||||||
|
|
||||||
//TODO: create workspace if it is not a signup to an existing workspace
|
//TODO: only create workspace if it is not a signup to an existing workspace
|
||||||
const workspaceDto: CreateWorkspaceDto = {
|
await this.workspaceService.create(user.id);
|
||||||
name: user.name, // will be better handled
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.workspaceService.create(workspaceDto, user.id);
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
findById(userId: string) {
|
async getUserInstance(userId: string) {
|
||||||
|
const user: User = await this.findById(userId);
|
||||||
|
const workspace: Workspace =
|
||||||
|
await this.workspaceService.getUserCurrentWorkspace(userId);
|
||||||
|
return { user, workspace };
|
||||||
|
}
|
||||||
|
|
||||||
|
async findById(userId: string) {
|
||||||
return this.userRepository.findById(userId);
|
return this.userRepository.findById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +49,7 @@ export class UserService {
|
|||||||
return this.userRepository.findByEmail(email);
|
return this.userRepository.findByEmail(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(id: number, updateUserDto: UpdateUserDto) {
|
async update(id: number, updateUserDto: UpdateUserDto) {
|
||||||
return `This action updates a #${id} user`;
|
return `This action updates a #${id} user`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ export class Workspace {
|
|||||||
@Column({ length: 255, nullable: true })
|
@Column({ length: 255, nullable: true })
|
||||||
logo: string;
|
logo: string;
|
||||||
|
|
||||||
@Column({ length: 255, unique: true })
|
@Column({ length: 255, nullable: true, unique: true })
|
||||||
hostname: string;
|
hostname: string;
|
||||||
|
|
||||||
@Column({ length: 255, nullable: true })
|
@Column({ length: 255, nullable: true })
|
||||||
|
|||||||
@ -7,4 +7,8 @@ export class WorkspaceRepository extends Repository<Workspace> {
|
|||||||
constructor(private dataSource: DataSource) {
|
constructor(private dataSource: DataSource) {
|
||||||
super(Workspace, dataSource.createEntityManager());
|
super(Workspace, dataSource.createEntityManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findById(workspaceId: string) {
|
||||||
|
return this.findOneBy({ id: workspaceId });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,15 +16,21 @@ export class WorkspaceService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(
|
async create(
|
||||||
createWorkspaceDto: CreateWorkspaceDto,
|
|
||||||
userId: string,
|
userId: string,
|
||||||
|
createWorkspaceDto?: CreateWorkspaceDto,
|
||||||
): Promise<Workspace> {
|
): Promise<Workspace> {
|
||||||
let workspace: Workspace = plainToInstance(Workspace, createWorkspaceDto);
|
let workspace: Workspace;
|
||||||
|
|
||||||
|
if (createWorkspaceDto) {
|
||||||
|
workspace = plainToInstance(Workspace, createWorkspaceDto);
|
||||||
|
} else {
|
||||||
|
workspace = new Workspace();
|
||||||
|
}
|
||||||
|
|
||||||
workspace.inviteCode = uuid();
|
workspace.inviteCode = uuid();
|
||||||
workspace.creatorId = userId;
|
workspace.creatorId = userId;
|
||||||
|
|
||||||
if (!workspace.hostname?.trim()) {
|
if (workspace.name && !workspace.hostname?.trim()) {
|
||||||
workspace.hostname = generateHostname(createWorkspaceDto.name);
|
workspace.hostname = generateHostname(createWorkspaceDto.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,4 +52,33 @@ export class WorkspaceService {
|
|||||||
|
|
||||||
return this.workspaceUserRepository.save(workspaceUser);
|
return this.workspaceUserRepository.save(workspaceUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findById(workspaceId: string): Promise<Workspace> {
|
||||||
|
return await this.workspaceRepository.findById(workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserCurrentWorkspace(
|
||||||
|
userId: string,
|
||||||
|
workspaceId?: string,
|
||||||
|
): Promise<Workspace> {
|
||||||
|
// TODO: use workspaceId and fetch workspace based on the id
|
||||||
|
// we currently assume the user belongs to one workspace
|
||||||
|
const userWorkspace = await this.workspaceUserRepository.findOne({
|
||||||
|
where: { userId: userId },
|
||||||
|
relations: ['workspace'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return userWorkspace.workspace;
|
||||||
|
}
|
||||||
|
|
||||||
|
async userWorkspaces(userId: string): Promise<Workspace[]> {
|
||||||
|
const workspaces = await this.workspaceUserRepository.find({
|
||||||
|
where: { userId: userId },
|
||||||
|
relations: ['workspace'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return workspaces.map(
|
||||||
|
(userWorkspace: WorkspaceUser) => userWorkspace.workspace,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user