forked from tsai/budibase
8 changed files with 155 additions and 35 deletions
@ -1,18 +1,54 @@ |
|||||
const env = require("../../environment") |
const env = require("../../environment") |
||||
|
const jwt = require("jsonwebtoken") |
||||
|
const database = require("../../db") |
||||
|
const { StaticDatabases, generateUserID } = require("../../db/utils") |
||||
|
|
||||
exports.options = { |
exports.options = { |
||||
clientId: env.GOOGLE_CLIENT_ID, |
clientID: env.GOOGLE_CLIENT_ID, |
||||
clientSecret: env.GOOGLE_CLIENT_SECRET, |
clientSecret: env.GOOGLE_CLIENT_SECRET, |
||||
callbackURL: env.GOOGLE_AUTH_CALLBACK_URL, |
callbackURL: env.GOOGLE_AUTH_CALLBACK_URL, |
||||
} |
} |
||||
|
|
||||
exports.authenticate = async function(token, tokenSecret, profile, done) { |
exports.authenticate = async function(token, tokenSecret, profile, done) { |
||||
console.log({ |
if (!profile._json.email) return done(null, false, "Email Required.") |
||||
token, |
|
||||
tokenSecret, |
// Check the user exists in the instance DB by email
|
||||
profile, |
const db = new database.CouchDB(StaticDatabases.GLOBAL.name) |
||||
done, |
|
||||
|
let dbUser |
||||
|
const userId = generateUserID(profile._json.email) |
||||
|
|
||||
|
try { |
||||
|
// use the google profile id
|
||||
|
dbUser = await db.get(userId) |
||||
|
} catch (err) { |
||||
|
console.error("Google user not found. Creating..") |
||||
|
// create the user
|
||||
|
const user = { |
||||
|
_id: userId, |
||||
|
provider: profile.provider, |
||||
|
roles: {}, |
||||
|
builder: { |
||||
|
global: true, |
||||
|
}, |
||||
|
...profile._json, |
||||
|
} |
||||
|
const response = await db.post(user) |
||||
|
|
||||
|
dbUser = user |
||||
|
dbUser._rev = response.rev |
||||
|
} |
||||
|
|
||||
|
// authenticate
|
||||
|
const payload = { |
||||
|
userId: dbUser._id, |
||||
|
builder: dbUser.builder, |
||||
|
email: dbUser.email, |
||||
|
} |
||||
|
|
||||
|
dbUser.token = jwt.sign(payload, env.JWT_SECRET, { |
||||
|
expiresIn: "1 day", |
||||
}) |
}) |
||||
// retrieve user ...
|
|
||||
// fetchUser().then(user => done(null, user))
|
return done(null, dbUser) |
||||
} |
} |
||||
|
|||||
@ -1,19 +1,17 @@ |
|||||
const Router = require("@koa/router") |
const Router = require("@koa/router") |
||||
const { passport } = require("@budibase/auth") |
const { passport } = require("@budibase/auth") |
||||
const authController = require("../controllers/auth") |
const authController = require("../controllers/auth") |
||||
|
const context = require("koa/lib/context") |
||||
|
|
||||
const router = Router() |
const router = Router() |
||||
|
|
||||
router |
router |
||||
.post("/api/admin/auth", authController.authenticate) |
.post("/api/admin/auth", authController.authenticate) |
||||
.post("/api/admin/auth/logout", authController.logout) |
|
||||
.get("/api/auth/google", passport.authenticate("google")) |
|
||||
.get( |
.get( |
||||
"/api/auth/google/callback", |
"/api/admin/auth/google", |
||||
passport.authenticate("google", { |
passport.authenticate("google", { scope: ["profile", "email"] }) |
||||
successRedirect: "/app", |
|
||||
failureRedirect: "/", |
|
||||
}) |
|
||||
) |
) |
||||
|
.get("/api/admin/auth/google/callback", authController.googleAuth) |
||||
|
.post("/api/admin/auth/logout", authController.logout) |
||||
|
|
||||
module.exports = router |
module.exports = router |
||||
|
|||||
Loading…
Reference in new issue