I use mac to develop a MEAN stack project. My web pages https://localhost:3000/#/login
and https://localhost:3000/#/new
work (note that all my pages need to have /#/
in the middle to work). https://localhost:3000/#/new
has a button that leads to a google login by passportjs
. In my routes ==> index.js
I set
var express = require('express');
var router = express.Router();
... ...
router.get('/auth/google', passport.authenticate('google', { scope: ['profile', 'email'] }));
router.get('/auth/google/callback',
passport.authenticate('google', { successRedirect: '/login',
failureRedirect: '/login' }));
In Google APIs ==> Credentials
, when I set https://localhost:3000/auth/google/callback
as Authorized redirect URIs
. Clicking on the login button on my page returns an error
Error: redirect_uri_mismatch
The redirect URI in the request, https://localhost:3000/auth/facebook/callback, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/367100934152-7csfhcdnkapil4obku1pr2cnrsmthk61.apps.googleusercontent.com?project=367100934152 to update the authorized redirect URIs.
I guess it is because https://localhost:3000/auth/facebook/callback
is not a valid URI for my app, but if I set https://localhost:3000/#/auth/facebook/callback
in the setting page, it is NOT allowed: Invalid Redirect: https://localhost:3000/#/auth/google/callback cannot contain a fragment.
So, does anyone know if I could modify my app to work without #
, so that https://localhost:3000/login
and https://localhost:3000/new
, etc. will work? As a result, https://localhost:3000/auth/facebook/callback
will be a valid URI too...
Edit 1:
Following the answer of @Sevran and the doc how to configure your server to work with html5mode, I did the following:
1) added $locationProvider.html5Mode(true)
in app.config
2) added <base href="/" />
in index.ejs
(note I don't have index.html
in the project)
3) keeps .state('new', url: '/new', ...
in app.config
4) did NOTHING in httpd-vhosts.conf
, which has <VirtualHost *:80>
and <VirtualHost *:433>
, because I think my server is controlled by express.js
rather than httpd-vhosts.conf
, and the port is 3000
.
5) did NOT add .htaccess
in the project like this answer
6) added in app.js
the following, I also tried to change index.html
to index.ejs
.
app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
The tests shows
entering
https://localhost:3000/#/new
in the URL bar becomeshttps://localhost:3000/new
, and it loads the corresponding page.entering
https://localhost:3000/new
in the URL bar raises anot found 404
error.entering
https://localhost:3000/auth/google
in the URL bar raises theError: redirect_uri_mismatch
as above.
To conclude, I think the rewriting (of a url without #
) should be managed by express, but the code I posted did not do the job. Regarding .htaccess
, I tried also to put it in the DocumentRoot
of httpd-vhosts.conf
or the root folder of the project, it did not help.