Home How to display a flash message in login page
Reply: 2

How to display a flash message in login page

Abhilash Shajan
1#
Abhilash Shajan Published in 2017-12-06 10:38:44Z

I'm using Grails 3.2.7 along with spring-security-core:3.1.1, I want to set a message to the flash object from a controller upon logout, then I need to display this message in my login screen (auth.gsp)

see the below code I wrote.

In controller

if(condition 1){ 
flash.message = "logged out successfully and your claim id is 12345"; redirect(uri:"/logout"); 
}else if(condition b){ 
flash.message = "logged out"; redirect(uri:"/logout"); 
}else if(condition c){ 
flash.message = "multiple login is not allowed"; redirect(uri:"/logout"); 
}

In the auth.gsp

<g:if test='${flash.message}'>
<div class="login_message">${flash.message}</div>
</g:if>

I could not see this message. Does anyone know how to display this message?

elixir
2#
elixir Reply to 2017-12-06 18:45:09Z

If you are not redirected to the auth page when you logout, you will not see the flash message.

Find out which page you are redirected to and this code.

<g:if test='${flash.message}'>
<div class="login_message">${flash.message}</div>
</g:if>

If you really want to see this message on the auth page, then make sure logout redirects you directly to the auth page.

Have you thought about using sessions?

http://docs.grails.org/3.1.1/ref/Servlet%20API/session.html

Unlike flash message, they exist until you reset them. Flash messages are reset after the first time you request them.

Sankar
3#
Sankar Reply to 2017-12-14 06:47:51Z

Answer for @Abilash

In your controller

    @RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout,
        @RequestParam(value = "expired", required = false) String expired,
        HttpServletRequest request) {

    ModelAndView model = new ModelAndView();
    if (error != null) {
        model.addObject("error", getErrorMessage(request, "SPRING_SECURITY_LAST_EXCEPTION"));
    }

    if (logout != null) {
        model.addObject("msg", "You've been logged out successfully.");
    }

    if (expired != null) {
        model.addObject("msg", "Session Timeout..!");

    }

    model.setViewName("login");

    return model;

}

And your View

<div class="error" style="color: red;" align="center">${error}</div>

In your spring security config

for Error

.and().formLogin().loginPage("/login").failureUrl("/login?error")

for Logount

.and().logout().logoutSuccessUrl("/login?logout").invalidateHttpSession(true) 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305977 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO