40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { AuthRepository } from './auth.repository';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class AuthService {
|
||
|
|
constructor(private readonly authRepository: AuthRepository) {}
|
||
|
|
|
||
|
|
async appleLogin(params: {
|
||
|
|
identityToken: string;
|
||
|
|
authorizationCode: string;
|
||
|
|
user?: { name?: { firstName?: string; lastName?: string }; email?: string };
|
||
|
|
}) {
|
||
|
|
const appleUserId = `apple_${params.identityToken.substring(0, 20)}`;
|
||
|
|
let user = await this.authRepository.findByAppleUserId(appleUserId);
|
||
|
|
if (!user) {
|
||
|
|
const displayName =
|
||
|
|
params.user?.name
|
||
|
|
? `${params.user.name.lastName || ''}${params.user.name.firstName || ''}`
|
||
|
|
: undefined;
|
||
|
|
user = await this.authRepository.createUser({
|
||
|
|
appleUserId,
|
||
|
|
email: params.user?.email,
|
||
|
|
displayName,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
const accessToken = `mock_token_${Date.now()}`;
|
||
|
|
const refreshToken = `mock_refresh_${Date.now()}`;
|
||
|
|
return { accessToken, refreshToken, expiresIn: 3600 };
|
||
|
|
}
|
||
|
|
|
||
|
|
async refresh(refreshToken: string) {
|
||
|
|
const accessToken = `mock_token_${Date.now()}`;
|
||
|
|
return { accessToken, expiresIn: 3600 };
|
||
|
|
}
|
||
|
|
|
||
|
|
async logout() {
|
||
|
|
return { success: true, message: '已退出登录' };
|
||
|
|
}
|
||
|
|
}
|