diff --git a/.env.example b/.env.example index e511c87..f488fbf 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,7 @@ PORT="8080" NODE_ENV="dev" CLIENT_ID="e92f22011eedd1157c6c4d92049fc072" -SECRET="edf6045fd704a1454404a302219bd4bb6fd1c579" \ No newline at end of file +SECRET="edf6045fd704a1454404a302219bd4bb6fd1c579" +CALLBACK_URL="http://localhost:5173/oauth-callback" +WIKIDATA_URL="https://www.wikidata.org/w/api.php" +ACCESS_TOKEN_URL="https://meta.wikimedia.org/w/rest.php/oauth2/access_token" \ No newline at end of file diff --git a/src/api/v1/modules/auth/auth.controller.ts b/src/api/v1/modules/auth/auth.controller.ts index 65c654c..4c9800b 100644 --- a/src/api/v1/modules/auth/auth.controller.ts +++ b/src/api/v1/modules/auth/auth.controller.ts @@ -8,9 +8,15 @@ export class AuthController { this.authService = new AuthService(); } - index: RequestHandler = (req, res) => { - return res.status(200).json({ - message: "Intergrade wikimedia OAuth 1.0a", - }); + index: RequestHandler = async (req, res) => { + const { code } = req.body; + const accessToken = await this.authService.login(code); + console.log("code", code); + + return await res.json(accessToken.body); + + // return res.status(200).json({ + // message: "Intergrade wikimedia OAuth 1.0a", + // }); }; } diff --git a/src/api/v1/modules/auth/auth.guard.ts b/src/api/v1/modules/auth/auth.guard.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/api/v1/modules/auth/auth.service.ts b/src/api/v1/modules/auth/auth.service.ts index 4c1dc30..8027904 100644 --- a/src/api/v1/modules/auth/auth.service.ts +++ b/src/api/v1/modules/auth/auth.service.ts @@ -1,5 +1,33 @@ +import { APP_CONF } from "../../../../config/app-config"; + export class AuthService { + headers: { "Content-Type": string }; constructor() { // + this.headers = { + "Content-Type": "application/x-www-form-urlencoded", + }; } + + // login + login = async (code: string) => { + const data = { + grant_type: "authorization_code", + code, + redirect_uri: APP_CONF.redirect_url, + client_id: APP_CONF.client_id, + client_secret: APP_CONF.client_secret, + }; + + const accessToken = await fetch(`${APP_CONF.access_token_url}`, { + method: "POST", + headers: this.headers, + body: JSON.stringify(data), + }); + console.log({ + accessToken: accessToken, + }); + + return accessToken; + }; } diff --git a/src/api/v1/routes/auth-router.ts b/src/api/v1/routes/auth-router.ts index f8f39c9..c1096fd 100644 --- a/src/api/v1/routes/auth-router.ts +++ b/src/api/v1/routes/auth-router.ts @@ -6,5 +6,6 @@ const router = Router(); const authController = new AuthController(); router.get("/", authController.index.bind(authController)); +router.post("/", authController.index.bind(authController)); export const authRouter = router; diff --git a/src/config/app-config.ts b/src/config/app-config.ts index e2dcb8e..db7928b 100644 --- a/src/config/app-config.ts +++ b/src/config/app-config.ts @@ -6,6 +6,8 @@ export const APP_CONF = { port: process.env.PORT!, env: process.env.NODE_ENV as "dev" | "prod", client_id: process.env.CLIENT_ID, - secret: process.env.SECRET, + client_secret: process.env.SECRET, wikiDataUrl: process.env.WIKIDATA_URL, + access_token_url: process.env.ACCESS_TOKEN_URL, + redirect_url: process.env.REDIRECT_URL, };