+ Start a Discussion
Sujendran Sundarraj 8Sujendran Sundarraj 8 

Custom login page using visualforce

Hello,
I am creating a simple custom login page with 2 fields username, password and a login button. 
I have created a new custom object - loginUsers to store the username and password. 
When I enter the credentials as per in that object it is not working and it is not showing any error message also. Please help me to fix this issue. 

VF: 
<apex:page controller="login" sidebar="false" showHeader="false" standardStylesheets="false">

<body style="background-image:url('{!$Resource.image}');"></body>
 <apex:form >
 
 <center style="margin-top:250px" >
 <apex:image url="{!$Resource.Nature}" width="250" height="100"/><br/>
 
 <br/>
  <apex:inputText id="Username" value="{!username}" /> <br/><br/>
 <apex:inputSecret id="password" value="{!password}"/>/><br/>

  <apex:commandButton value="login" action="{!login}"/>
  <apex:commandLink value="forgetpassword"/>

 </center>
 </apex:form>
</apex:page>

Apex class: 
public class login {
    
    public string username {get; set;}
    public string password {get; set;}
    public list<loginUsers__C> u =[select name,password__C from loginUsers__c where name = :username and password__c =:password];
   
   
 
    public pagereference login()
        {
        if(u.size()>0){
                 pagereference pgref = new pagereference('/apex/loginsf1');
            pgref.setredirect(true);
                 return pgref ;
        }
       
         else
         apexpages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'please enter the username and password'));

         return null;
        }
  
    
}

Thank you very much in advance. 
Regards, 
Sujendran. 
 
FARSANA PSFARSANA PS
Hi,
Try this..
public class login {
    
    public string username {get; set;}
    public string password {get; set;}
  
   
 
    public pagereference login()
        {
          public list<loginUsers__C> u =[select name,password__C from loginUsers__c where name = :username and password__c =:password];
        if(u.size()>0){
                 pagereference pgref = new pagereference('/apex/loginsf1');
            pgref.setredirect(true);
                 return pgref ;
        }
       
         else
        {
         apexpages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'please enter the username and password'));

         return null;
        }
     }
  
    
}

Hope this helps...
Sujendran Sundarraj 8Sujendran Sundarraj 8
Hello Farsana, 
Thank you for your swift response. 

I found it, We need to add question mark at the end of the pagelink as below
pagereference pgref = new pagereference('/apex/loginsf1?');