Authenticating into Node-RED using Google OAuth - Working Solution

#1

Saw some questions in the forum about authenticating into Node-RED using Google OAuth, but couldn't find a working example.
So here is the code after I got it working with passport-google-oauth.

Please let me know if this works for you or not.

adminAuth: {
  type: "strategy",
  strategy: {
    name: "google",
    label: 'Sign in with Google',
    icon: "fa-google",
    strategy: require('passport-google-oauth').OAuth2Strategy,
    options: {
      clientID: 'some client id',
      clientSecret: 'some client secret',
      scope: 'email',
      callbackURL: "https://your-node-red-instance.com/auth/strategy/callback",
      verify: function (token, tokenSecret, profile, done) {
        profile.username = profile.emails.find(x => x.verified).value;
        done(null, profile);
      }
    }
  },
  users: function (username) {
    return Promise.resolve({
      username: username,
      permissions: "*"
    });
  }
}
1 Like

#2

Does this need any settings in Google API's? Also, does it need a callback URL that is actually accessible by Google? (quite a lot of these API's don't).

0 Likes

#3

Yes, this required generating a ClientId and ClientSecret at
https://console.developers.google.com/apis/credentials.
The "Authorized redirect URIs" I entered there must match the callbackURL that I set in the strategy options in Node-RED.

Adding screenshots of the admin settings below.

Regarding whether the callback really needs to be accessible by Google, I'm afraid I don't know.
My Node-RED instance is publicly available but I don't know if this is a must.

1 Like