Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to login page if user not logged in

Tags:

I want my user to be redirected to the login form if he is not logged in ?

The thing is that I don't wanna do this for every action. I know there is a way so that I may define this for every controller only once ( i.e. something executes regardless of the action in my controller ) but I can't remember how.

like image 711
Alex Avatar asked Feb 20 '11 10:02

Alex


People also ask

How do you redirect the login page when user is not logged in?

Set a session after login successful and in edit. php check for it, if session is not set, redirect it to homepage. Show activity on this post. Show activity on this post.

How do I redirect to login page if user is not logged in WordPress?

function login_redirect() { // Current Page global $pagenow; // Check to see if user in not logged in and not on the login page if(! is_user_logged_in() && $pagenow != 'wp-login. php') // If user is, Redirect to Login form.

How do I redirect to login page if not logged in in react?

Path: /src/_components/PrivateRoute.jsx If the user is logged in the Component prop is rendered, otherwise if the user is not logged in the React Router Redirect component is rendered which redirects the user to the /login page. The requested path ( props. location ) is passed with the redirect in the state.


1 Answers

Use a before_filter. Place the following in your application controller.

# application_controller.rb  before_filter :require_login  private    def require_login     unless current_user       redirect_to login_url     end   end 

The code above assumes you have defined a method current_user which returns the user record when the user is logged in.

Then, in your login controller

skip_before_filter :require_login 
like image 58
Simone Carletti Avatar answered Nov 02 '22 19:11

Simone Carletti