-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { userJsonSchema } from './user-props.js' | ||
import { getUserQuery } from './user-query.js' | ||
|
||
export async function getUser (fastify, opts) { | ||
fastify.get( | ||
'/', | ||
{ | ||
preHandler: fastify.auth([fastify.verifyJWT]), | ||
schema: { | ||
response: { | ||
200: userJsonSchema | ||
} | ||
} | ||
}, | ||
async function (request, reply) { | ||
const userId = request.user.id | ||
|
||
const query = getUserQuery({ userId }) | ||
|
||
const results = await fastify.pg.query(query) | ||
const user = results.rows.pop() | ||
|
||
if (user) { | ||
// TODO refresh token | ||
return user | ||
} else { | ||
reply.statusCode = 404 | ||
} | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,7 @@ | ||
import SQL from '@nearform/sql' | ||
import S from 'fluent-json-schema' | ||
|
||
const userJsonSchema = S.object() | ||
.prop('id', S.string().format('uuid')) | ||
.prop('email', S.string().format('email')) | ||
.prop('username', S.string()) | ||
.prop('email_confirmed', S.boolean()) | ||
import { getUser } from './get-user.js' | ||
|
||
export default async function userRoutes (fastify, opts) { | ||
fastify.get( | ||
'/', | ||
{ | ||
preHandler: fastify.auth([fastify.verifyJWT]), | ||
schema: { | ||
response: { | ||
200: userJsonSchema | ||
} | ||
} | ||
}, | ||
async function (request, reply) { | ||
const id = request.user.id | ||
|
||
const query = SQL` | ||
SELECT id, email, username, email_confirmed | ||
FROM users | ||
WHERE id = ${id} | ||
` | ||
|
||
const results = await fastify.pg.query(query) | ||
const user = results.rows.pop() | ||
|
||
if (user) { | ||
// TODO refresh token | ||
return user | ||
} else { | ||
reply.statusCode = 404 | ||
} | ||
} | ||
) | ||
await Promise.all([ | ||
getUser(fastify, opts) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import S from 'fluent-json-schema' | ||
|
||
export const userJsonSchema = S.object() | ||
.prop('id', S.string().format('uuid')) | ||
.prop('email', S.string().format('email')) | ||
.prop('username', S.string()) | ||
.prop('email_confirmed', S.boolean()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import SQL from '@nearform/sql' | ||
|
||
export function getUserQuery ({ | ||
userId | ||
}) { | ||
const query = SQL` | ||
select id, email, username, email_confirmed | ||
from users | ||
where id = ${userId}` | ||
|
||
return query | ||
} |