Get user by id

This commit is contained in:
Amit Jakubowicz 2018-09-29 15:25:11 +02:00
parent febe02016e
commit 3a17e6623a
3 changed files with 21 additions and 3 deletions

View file

@ -163,6 +163,25 @@ export default class Repository {
})
}
async getUserById(id: string, datastore?: Datastore | DatastoreTransaction): Promise<User> {
const ds = (datastore || this.datastore)
const key = this.datastore.key(['User', parseInt(id)])
console.log('repository getUserById', id, key)
const result = await ds.get(key)
if (!result) {
return null
} else if (result.length > 1) {
const message = `Got more than one even for the same queried id ${id}`
throw new Error(message)
}
console.log('repository.getUserById succeeded with result', JSON.stringify(result));
const user = result[0] as User
user.id = user[Datastore.KEY].id
return result[0] as User;
}
async getEvent(id: string, datastore ?: Datastore | DatastoreTransaction): Promise<CalendarEvent> {
const ds = (datastore || this.datastore)
const key = this.datastore.key(['Event', parseInt(id)])

View file

@ -85,9 +85,7 @@ export default class SessionManager {
if (sessionInvite.timeValidated) {
return Promise.reject('Session request has already been validated')
}
const matchingUser: User = await this.repository.getUser({
email: sessionRequest.email
})
const matchingUser: User = await this.repository.getUserById(sessionInvite.userId)
if (matchingUser.id === sessionInvite.userId) {
const session: Session = {
userId: matchingUser.id,

View file

@ -83,6 +83,7 @@ const handleSignin = async (req: Request, res: Response) => {
const ip = req.ip.split('.').map(num => parseInt(num))
console.log('Got sign in request with params', JSON.stringify(params))
const session: Session = await sessionManager.initiateSession({
hash: params.hash as string,
email: params.email as string,