mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat(api): user authentication
This commit is contained in:
parent
f14eda16ac
commit
f253a66f86
41 changed files with 657 additions and 338 deletions
42
src/users/users.service.ts
Normal file
42
src/users/users.service.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { CreateOrUpdateDto } from "./dto/create-or-update.dto";
|
||||
import { User } from "./user.entity";
|
||||
import { UserRepository } from "./user.repository";
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly userRepository: UserRepository) {}
|
||||
|
||||
async findById(id: string): Promise<User> {
|
||||
const user = await this.userRepository.findOne(id);
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException("UserNotFound");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async createOrUpdate(data: CreateOrUpdateDto): Promise<User> {
|
||||
let user = await this.userRepository.findOne({
|
||||
where: { spotify: { id: data.spotify.id } }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
user = this.userRepository.create({
|
||||
spotify: {
|
||||
id: data.spotify.id
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
user.spotify.accessToken = data.spotify.accessToken;
|
||||
user.spotify.refreshToken = data.spotify.refreshToken;
|
||||
user.displayName = data.displayName;
|
||||
user.photo = data.photo;
|
||||
|
||||
await this.userRepository.save(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue