-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[optimize] update Read Me documents
- Loading branch information
Showing
9 changed files
with
90 additions
and
19 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
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,6 +1,6 @@ | ||
{ | ||
"name": "@idea2app/rest-node-ts", | ||
"version": "0.9.0", | ||
"version": "1.0.0", | ||
"license": "LGPL-3.0", | ||
"author": "[email protected]", | ||
"description": "RESTful API service scaffold based on Node.js & TypeScript", | ||
|
@@ -45,6 +45,7 @@ | |
"web-utility": "^4.4.0" | ||
}, | ||
"devDependencies": { | ||
"@octokit/openapi-types": "^22.2.0", | ||
"@types/jsonwebtoken": "^9.0.6", | ||
"@types/koa": "^2.15.0", | ||
"@types/koa-logger": "^3.1.5", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
import { Body, JsonController, Post } from 'routing-controllers'; | ||
import { ResponseSchema } from 'routing-controllers-openapi'; | ||
import { isDeepStrictEqual } from 'util'; | ||
|
||
import { dataSource, GitHubUser, OAuthSignInData, User } from '../model'; | ||
import { ActivityLogController } from './ActivityLog'; | ||
import { UserController } from './User'; | ||
|
||
const store = dataSource.getRepository(User); | ||
|
||
@JsonController('/user/OAuth') | ||
export class OauthController { | ||
@Post('/GitHub') | ||
@ResponseSchema(User) | ||
async signInWithGithub(@Body() { accessToken }: OAuthSignInData) { | ||
const response = await fetch('https://api.github.com/user', { | ||
headers: { Authorization: `Bearer ${accessToken}` } | ||
}); | ||
const { email, login, avatar_url } = | ||
(await response.json()) as GitHubUser; | ||
const user = | ||
(await store.findOneBy({ email })) || | ||
(await UserController.signUp({ email, password: accessToken })); | ||
const newProfile = { name: login, avatar: avatar_url }, | ||
oldPofile = { name: user.name, avatar: user.avatar }; | ||
|
||
if (!isDeepStrictEqual(oldPofile, newProfile)) { | ||
await store.update(user.id, newProfile); | ||
|
||
await ActivityLogController.logUpdate(user, 'User', user.id); | ||
} | ||
return UserController.sign(user); | ||
} | ||
} |
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
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,9 @@ | ||
import { components } from '@octokit/openapi-types'; | ||
import { IsString } from 'class-validator'; | ||
|
||
export class OAuthSignInData { | ||
@IsString() | ||
accessToken: string; | ||
} | ||
|
||
export type GitHubUser = components['schemas']['simple-user']; |
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
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
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