Skip to content

Commit

Permalink
| #5 | @Gmarvis | login with wikimedia oauth2 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmarvis committed Aug 28, 2024
1 parent a0812d0 commit 6ec211d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
PORT="8080"
NODE_ENV="dev"
CLIENT_ID="e92f22011eedd1157c6c4d92049fc072"
SECRET="edf6045fd704a1454404a302219bd4bb6fd1c579"
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"
14 changes: 10 additions & 4 deletions src/api/v1/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
// });
};
}
Empty file.
28 changes: 28 additions & 0 deletions src/api/v1/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
1 change: 1 addition & 0 deletions src/api/v1/routes/auth-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion src/config/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 6ec211d

Please sign in to comment.