Skip to content

Commit

Permalink
update sign in with social, get solana addr
Browse files Browse the repository at this point in the history
  • Loading branch information
truongnguyenptn committed Oct 24, 2023
1 parent ae879c0 commit e37a30f
Showing 1 changed file with 54 additions and 76 deletions.
130 changes: 54 additions & 76 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,62 +76,46 @@ export class AuthService {

async signInWithSocial({ publicKey, role }: SignInWithSocialDto, authorization: string): Promise<ReturnAccountDto> {
const idToken = authorization.replace('Bearer ', '')
console.log("test1",idToken)

try {
const decodedToken: TokenPayload = this.jwtService.decode(idToken) as TokenPayload
if (!decodedToken) {
throw new ForbiddenException(Message.Base.AccessDenied())
}
console.log("decode",decodedToken)
const { address } = await this.getSolanaAddress(authorization)

if (decodedToken?.jti) {
const person = await this.personRepository.findOne({ where: { publicKey: decodedToken.jti } })
console.log("test2")

const person = await this.personRepository.findOne({ where: { publicKey: address} })
if (!person) {

const url = 'https://filmatron-client-a88cb9.kylan.so/api/user'
console.log("test3")
fetch(url, {
const response = await fetch(url, {
method: 'GET',
headers: {
'Access-Control-Allow-Origin': '*',
Authorization: authorization
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok')
}
console.log(response.json())
return response.json()
})
.then(async data => {
const rolePerson = await this.roleRepository.findOne({ where: { role } })
console.log({authorization})
const { address } = await this.getSolanaAddress(authorization)

await this.personRepository.save({
publicKey: address,
email: data.email,
avatar: data.image,
name: data.name,
rolePerson
})
console.log("create person succesfull");
})
.catch(() => {
throw new UnauthorizedException(Message.Base.NotFound('Authorization invalid'))
})
}

return {
accessToken: idToken,
person,
refreshToken: 'testRefresh'
}
Authorization: authorization,
},
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

const data = await response.json();
const rolePerson = await this.roleRepository.findOne({ where: { role } });

await this.personRepository.save({
publicKey: address,
email: data.email,
avatar: data.image,
name: data.name,
rolePerson,
});


}
} catch {
return {
accessToken: idToken,
person,
refreshToken: 'testRefresh',
};
} catch (error) {
console.log(error);
throw new UnauthorizedException(Message.Base.NotFound('Token invalid'))
}
}
Expand Down Expand Up @@ -202,41 +186,35 @@ export class AuthService {
}

async getSolanaAddress(authorization: string): Promise<ReturnSolanaAddressDto> {
console.log(authorization)
let solanaAdress = '';
if (authorization) {
const url = 'https://filmatron-client-a88cb9.kylan.so/api/user/address/solana'

await fetch(url, {
console.log(authorization);

if (!authorization) {
return { address: '' };
}

try {
const url = 'https://filmatron-client-a88cb9.kylan.so/api/user/address/solana';
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${authorization}`
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok')
}
return response.json()
})
.then(data => {
solanaAdress = data.address;
})
.catch(() => {
throw new UnauthorizedException(Message.Base.NotFound('Token invalid'))
})
}

if (solanaAdress) {
return {
address: solanaAdress
Authorization: authorization,
},
});

if (!response.ok) {
throw new Error('Network response was not ok');
}
} else {

const data = await response.json();
return {
address: ''
}
address: data.address,
};
} catch (error) {
console.log(error);
throw new UnauthorizedException(Message.Base.NotFound('Token invalid'));
}
}


hashData(data: string) {
return bcrypt.hashSync(data, config.bcrypt.salt)
Expand Down

0 comments on commit e37a30f

Please sign in to comment.