Possible to log in specific user with Passport Strava strategy without log in screen?

Viewed 7

I want to have an online map which publicly loads and shows all my activities from my Strava account.

I have found a web app on GitHub which does what I want but the user has to log in with the Strava log in screen before he than can see his own activities: https://github.com/nsynes/ActivityMap

It seems it uses Passport Strava to authenticate with Strava: https://www.passportjs.org/packages/passport-strava-oauth2/

Is it possible to adjust the script so it always logs in automatically my account and than shows my activities publicly to everyone who visits the map?

The full script is here: https://github.com/nsynes/ActivityMap/blob/master/activity-map-app.js

My STRAVA_CLIENT_ID and STRAVA_CLIENT_SECRET are from my Strava account and saved in a .env file.

const express = require('express')
const passport = require('passport')
const util = require('util')
const StravaStrategy = require('passport-strava-oauth2').Strategy
const dotenv = require('dotenv');
const path = require('path');
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser');
const session = require('express-session');
var strava = require('strava-v3');
const decode = require('geojson-polyline').decode
const geodist = require('geodist');

dotenv.config();

const port = process.env.PORT || 3000
const app = express();


// configure Express
//app.use(express.logger());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
//app.use(express.methodOverride());
app.use(session({
    secret: 'monkey tennis',
    resave: true,
    saveUninitialized: true,
    maxAge: 1800 * 1000
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/css', express.static(path.join(__dirname, 'css')));
app.use('/fontawesome', express.static(path.join(__dirname, 'fontawesome')));
app.use('/js', express.static(path.join(__dirname, 'js')));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

// Passport session setup.
passport.serializeUser(function(user, done) { done(null, user) });
passport.deserializeUser(function(obj, done) {done(null, obj) });

passport.use(new StravaStrategy({
    clientID: process.env.STRAVA_CLIENT_ID,
    clientSecret: process.env.STRAVA_CLIENT_SECRET,
    callbackURL: "/auth/strava/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      // To keep the example simple, the user's Strava profile is returned to
      // represent the logged-in user.  In a typical application, you would want
      // to associate the Strava account with a user record in your database,
      // and return that user instead.
      return done(null, profile);
    });
  }
));

app.get('/', ensureAuthenticated, function(req, res){
    pagePath = path.join(__dirname, '/index.html');
    res.sendFile(pagePath);
});

app.get('/userPhoto', ensureAuthenticated, function(req, res){
    if ( req.user ) {
        res.json({ 'photo': req.user.photos[req.user.photos.length-1].value });
    } else {
        res.sendStatus(404);
    }
});

// Use passport.authenticate() as route middleware to authenticate the
// request.  Redirect user to strava, then strava will redirect user back to
// this application at /auth/strava/callback
app.get('/auth/strava',
  passport.authenticate('strava', { scope: ['activity:read'], approval_prompt: ['force'] }),
  function(req, res){
    // The request will be redirected to Strava for authentication, so this
    // function will not be called.
  });

// GET /auth/strava/callback
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  If authentication fails, the user will be redirected back to the
//   login page.  Otherwise, the primary route function function will be called,
//   which, in this example, will redirect the user to the home page.
app.get('/auth/strava/callback',
  passport.authenticate('strava', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

app.get('/logout', function(req, res){
  req.logout();
  res.cookie("connect.sid", "", { expires: new Date() });
  res.render('login', { user: req.user });
});

app.get('/login', function(req, res){
    res.render('login', { user: req.user });
  });
0 Answers
Related