Search code examples
javagrailsgroovy

Not able to login after user is created in grails


Acutally i create a user from the createorg form and it is also saved in database but when i try to login from that username and password it says me error that such username and password cannot be found.But the user that i have saved from bootstrap can be use to login easily.

createorg.gsp

  <g:form action="saveorg" method="POST">
                    <h3>Enter Username</h3>
                    <g:textField name="username" placeholder="Enter UserName"/><br>
                    <h3>Enter Password</h3>
                    <g:textField name="password" id="pass1" placeholder="Enter Password" /><br>
                     <h3>Confirm Password</h3>
                     <g:textField name="password" id="pass2" placeholder="Confirm Password" onkeyup='check();'/><br>

                 <div class="alert alert-success" style="display: none" id="showAlert">
                    <strong><span id='message'></span></strong>
                </div>
                <div class="alert alert-success" style="display: none" id="showAlert1">
                    <strong><span id='message1'></span></strong>
                </div>

                 <g:submitButton  class="btn btn-primary" name="saveorg" value="saveorg" id="submit" />

            </g:form>

organizationcontroller.groovy

@Secured('permitAll')
    def saveorg(User user) {
        if (user == null) {
            notFound()
            return
        }

        try {
            userService.save(user)
            println("i m here in orgcontroller")
            new UserRole(user:user,role:Role.findByAuthority('ROLE_ORG')).save(flush: true, failOnError: true)
            redirect(action: "anotherpage")
        } catch (ValidationException e) {
            println(e.getMessage())
            respond user.errors, view:'create'
            return
        }


    }

Solution

  • @Transactional
    def save(User user) {
        if (user == null) {
            transactionStatus.setRollbackOnly()
            render status: NOT_FOUND
            return
        }
    
        if (user.hasErrors()) {
            transactionStatus.setRollbackOnly()
            render(template: "/errors/errors", model: [errors: user.errors])
            return
        }
    
        user.save flush:true
    
        respond user, [status: CREATED, view:"show"]
    }