Login Application

Posted by Anantha Narayanan On 7:40 PM 7 comments

Create an application

C:\GRAILS_APPLICATONS>grails create-app login-test

Now create a controller for the user to show login buttons, or once logged in we need to show some message.

C:\GRAILS_APPLICATONS\LOGIN-TEST>grails create-controller useraccount

Once the controller is created, create a gsp file index.gsp within C:GRAILS_APPLICATION\login-test\grails-app\views\useraccount. Call it index.gsp. We want this controller because by default the controller will have a def index = { } in its body. To use this we are creating the gsp with same name. You are always free to modify the def index to some name you choose for your login, say home. Below is the content of the gsp file

<html>
<head>
<title>Welcome to Grails</title>
<meta name="layout" content="main" />
<style type="text/css" media="screen">
#login{
background-color:darkred;
color:white;
text-align:right;
padding:5px 15px;
height:25px;
}
#login a{
color:white;
}
</style>
</head>
<body>
<div id="login">
<g:if test="${session.user}">
<g:if test="${flash.message}">Welcome back ${flash.message}</g:if>
<g:else>Logged in as <B>${session.user}</B></g:else>
 | <g:link action="logout">Logout</g:link>
</g:if>
<g:else>
<g:form controller="useraccount" action="login">
<label for="name">Username</label>
<input type="text" name="username"/>
<label for="password">Password</label>
<input type="password" name="password"/>
<input type="submit" value="Login"/>
</g:form>
</g:else>
</div>
</body>
</html>

Now modify your controller file (UseraccountController.groovy) as below:

package login.test
class UseraccountController {
def index = { }
def login ={
if (params.username == "grailslover" && params.password == "hi"){
flash.message = "Welcome back, <B>${params.username}</B>."
session.user = "grailslover"
}else{
flash.message = "Login failed"
}
redirect(action: 'index')
}
def logout ={
session.user = null
redirect(action: 'index')
}
}

Now run your application using grails run-app and point your browser to this URL:
http://localhost:8080/login-test/useraccount/index

Login with username grailslover and password as hi. Initially when you login you will see a message Welcome back grailslover along with a link for Logout. Refresh the page and the message changes to Logged in as grailslover.

Now click on Logout the Login page with placeholders for Username and password appears.

To Download the application, click here. If you like the tutorial, please leave your valuable comments below.

7 comments:

its meant for beginners like me :)

Superb simple example Narayanan and i am the beginner for the grails so would like to learn this more......DOUBT.
whats the difference between flash. message and render "login success" and return [message:'login success']

I am totally clueless on that +manikandan s

nice article, simple explanation

We can use the default spring authentication for login can't we?!!

Post a Comment

Recommended Post Slide Out For Blogger