mirror of https://github.com/Budibase/budibase.git
8 changed files with 155 additions and 35 deletions
@ -1,18 +1,54 @@ |
|||
const env = require("../../environment") |
|||
const jwt = require("jsonwebtoken") |
|||
const database = require("../../db") |
|||
const { StaticDatabases, generateUserID } = require("../../db/utils") |
|||
|
|||
exports.options = { |
|||
clientId: env.GOOGLE_CLIENT_ID, |
|||
clientID: env.GOOGLE_CLIENT_ID, |
|||
clientSecret: env.GOOGLE_CLIENT_SECRET, |
|||
callbackURL: env.GOOGLE_AUTH_CALLBACK_URL, |
|||
} |
|||
|
|||
exports.authenticate = async function(token, tokenSecret, profile, done) { |
|||
console.log({ |
|||
token, |
|||
tokenSecret, |
|||
profile, |
|||
done, |
|||
if (!profile._json.email) return done(null, false, "Email Required.") |
|||
|
|||
// Check the user exists in the instance DB by email
|
|||
const db = new database.CouchDB(StaticDatabases.GLOBAL.name) |
|||
|
|||
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 { passport } = require("@budibase/auth") |
|||
const authController = require("../controllers/auth") |
|||
const context = require("koa/lib/context") |
|||
|
|||
const router = Router() |
|||
|
|||
router |
|||
.post("/api/admin/auth", authController.authenticate) |
|||
.post("/api/admin/auth/logout", authController.logout) |
|||
.get("/api/auth/google", passport.authenticate("google")) |
|||
.get( |
|||
"/api/auth/google/callback", |
|||
passport.authenticate("google", { |
|||
successRedirect: "/app", |
|||
failureRedirect: "/", |
|||
}) |
|||
"/api/admin/auth/google", |
|||
passport.authenticate("google", { scope: ["profile", "email"] }) |
|||
) |
|||
.get("/api/admin/auth/google/callback", authController.googleAuth) |
|||
.post("/api/admin/auth/logout", authController.logout) |
|||
|
|||
module.exports = router |
|||
|
|||
Loading…
Reference in new issue