-
Notifications
You must be signed in to change notification settings - Fork 59
Redirect URL
I have a Jquery Powered Login box in my base template which means users can login from any url inside the application. I want the users to be redirected back to the page they requested to login from. Is there any way we can achieve this with Oauth2 or can we just redirect users back to only a specific url ??
If you're using SimpleAuth I assume you're probably with webapp2, so @jmort253 examples are not exactly how I would do it (e.g. webapp2 has built-in sessions, so why use yet another library for sessions handling).
Though, conceptually it is correct: what you need is store original URL somewhere in a session before starting authentication process. Then use that stored URL for a final redirect, after successful authentication.
Starting from the example app code of SimpleAuth, what you basically need to change is the last line of _on_signin() to redirect users to that original URL they came from (instead of '/profile').
To store the original request URL you could use a simple wrapper, e.g.
def simpleauth_login_required(handler_method):
"""A decorator to require that a user be logged in to access a handler.
To use it, decorate your get() method like this:
@simpleauth_login_required
def get(self):
user = self.current_user
self.response.out.write('Hello, ' + user.name())
"""
def check_login(self, *args, **kwargs):
if self.request.method != 'GET':
self.abort(400, detail='The login_required decorator '
'can only be used for GET requests.')
if self.logged_in:
handler_method(self, *args, **kwargs)
else:
self.session['original_url'] = self.request.url
self.redirect('/my-login-page-where-users-can-choose-auth-method')
return check_loginNow, going back to that _on_signin() redirect line, instead of self.redirect('/profile') you'd do something like this:
target = self.session['original_url']
self.redirect(target)A couple notes:
- the example above assumes you have a
logged_inmethod which indicates whether the current request is made by an already authenticated user or not; - you'll probably want to clear 'original_url' from the session (if they successfully authenticated)
The above example's credits go to webapp2_extras.appengine.users module.