Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
wip OAuth support using 'grant' module. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 1, 2019
1 parent 841d942 commit c410c53
Show file tree
Hide file tree
Showing 16 changed files with 512 additions and 340 deletions.
46 changes: 44 additions & 2 deletions express/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import express, { Response } from "express";
import jwt from "express-jwt";

import { authenticate, getOnlineFriends, sendFriendRequest, connectDatabase, getFriends, getFriendRequests, getFriendRequestsProfile, consumeFriendRequest, assignDeviceToUser, pingUser, blockUser, unblockUser, updateUser, getOnlineUserCount } from "../src";
import User from "../src/models/User";
import { authenticate, getOnlineFriends, sendFriendRequest, getFriends, getFriendRequests, getFriendRequestsProfile, consumeFriendRequest, assignDeviceToUser, pingUser, blockUser, unblockUser, updateUser, getOnlineUserCount, IUser } from "../src";
import { connectDatabase, User } from "../src";
import * as providers from "../src/providers";

import { JWT_SECRET } from "../src/env";
import { AuthDataInToken, createToken } from "../src/auth";
Expand Down Expand Up @@ -54,6 +55,8 @@ auth.post("/", async (req, res) => {
const { accessToken, deviceId, platform, token, email, password } = req.query;

const user = await authenticate({ accessToken, deviceId, platform, token, email, password });

// assign deviceId & platform
if (deviceId && platform) {
await assignDeviceToUser(user, deviceId, platform);
}
Expand All @@ -62,6 +65,45 @@ auth.post("/", async (req, res) => {
}, 401);
});

auth.get("/callback", (req, res) => {
const { grant } = (req as any).session;
tryOrErr(res, async () => {
const provider = grant.provider;
const raw = grant.response.raw;

// const providerId = raw.user_id;
// const displayName = raw.screen_name;

const options: any = { provider };

if (providers[provider]) {
const data = await providers[provider](raw);
}
if (provider === "facebook") {
options.accessToken = raw.access_token;
}

// const user = await authenticate(options);
// res.json({ ...user.toJSON(), ...createToken(user) });

// TODO: how to handle authentication from a native mobile app?

res.send(`<html><head>
<script type="text/javascript">
window.opener.postMessage(${JSON.stringify(raw)}, "*")
window.close();
</script>
</head></html>`);

// res.json({
// provider,
// response: grant.response
// });


}, 401);
});

auth.put("/", jwtMiddleware, express.json(), async (req, res) => {
tryOrErr(res, async () => {
res.json({ status: await updateUser(req.cauth._id, req.body) });
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/social",
"version": "0.11.5",
"version": "0.11.6",
"description": "Authentication and Social features for Colyseus",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"@types/mongoose": "^5.3.27",
"debug": "^4.1.1",
"grant": "^4.6.3",
"httpie": "^1.1.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.5.6",
Expand All @@ -55,13 +56,15 @@
"express": "^4.16.4",
"express-basic-auth": "^1.2.0",
"express-jwt": "^5.3.1",
"express-session": "^1.16.2",
"mocha": "^6.1.4",
"mongodb": "^3.2.3",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
},
"peerDependencies": {
"express": "^4.16.4",
"express-jwt": "^5.3.1"
"express-jwt": "^5.3.1",
"express-session": "^1.16.2"
}
}
22 changes: 22 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose";
import { MONGO_URI } from "./env";
import { MongoError } from "mongodb";
import { debug } from "./index";

export async function connectDatabase(cb?: (err: MongoError) => void) {
// skip if already connecting or connected.
if (mongoose.connection.readyState !== 0) {
if (cb)
cb(null);
return;
}
try {
await mongoose.connect(MONGO_URI, { autoIndex: false, useNewUrlParser: true, useUnifiedTopology: true }, cb);
debug(`Successfully connected to ${MONGO_URI}`);
// reconnect if disconnected.
mongoose.connection.on('disconnected', () => connectDatabase());
}
catch (e) {
console.error('Error connecting to database: ', e);
}
}
13 changes: 0 additions & 13 deletions src/facebook.ts

This file was deleted.

Loading

0 comments on commit c410c53

Please sign in to comment.