function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
B TulasiB Tulasi 

Login Page using VF

Hi All,

I am trying to create One LoginPage using Visualforce Page. But, I am getting error in this. 

Requirement :

1 > Username is corrct and Password is correct
    It needs to login the page
2 > Username is Wrong and Password is correct
    Error : Plz Enter Valid Username
3 > Username is correct and Password is wrong
    Error : Plz Enter Valid Password
4 > Username is wrong and Password is wrong
    Error : Plz Enter Valid Username and Password.

I am getting unexpected Results. 


VF Code :

<apex:page controller="Sample">
<apex:form >
<apex:pageBlock >

    <br>UserName&nbsp;<apex:inputText label="center" value="{!userName}"/></br>
    <br>Password &nbsp;&nbsp;<apex:inputsecret value="{!password}"/></br>
    <apex:commandButton value="Login" action="{!loginPage}" reRender="Error"/>
    
    <apex:pageMessages id="Error"></apex:pageMessages> 
    
</apex:pageBlock>
</apex:form>
</apex:page>


My code : 


public class Sample {      
    public String userName{get;set;}
    public String password{get;set;}
    public List<Account> returns {get;set;}
    public Sample(){
        returns =new List<Account>();
    }
    public PageReference loginPage(){
        
        Account acc=new Account();
       returns = [select ID, Name, Sic from Account Where Name=:userName and sic=:password];
        if(userName == acc.Name && Password == acc.Sic){
            Pagereference pr = new PageReference('/'+returns[0].Id);
            pr.setReDirect(true);
            return pr;
        }
        
        else if (userName != acc.Name && Password == acc.Sic){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Plz Enter valid UserName'));
            return null;

        }
        else if ( userName == acc.Name  && Password != acc.Sic){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Plz Enter Valid Password'));
            return null;
        }
        
        else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid UserName or Password'));
                return null;
        }
        
    }

}


Thanks in Advance 
Thulasi
William TranWilliam Tran
Try replacing this:
returns = [select ID, Name, Sic from Account Where Name=:userName and sic=:password];

with
Acc= [select ID, Name, Sic from Account Where Name=:userName and sic=:password];

if you get an error then declare Accs as a List<account> then use
Accs = [select ID, Name, Sic from Account Where Name=:userName and sic=:password];
Acc = Accs[0];

In summary you never set the Acc so it is alway blank.

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks
Luke GreenstreetLuke Greenstreet
Hi,

I am new to coding and Apex,
I have attempted to use this to build my own solution.
From the login page the Username and Password should be entered,
These are stored on the account object in Loan_Application_Username__pc, Loan_Application_Password__pc. After succesful login the page should redirect to an info capture VF page called OnlineLoanApplication which uses an Account Standard controller.

With the reading I have done around the internet and from your comments I have built an empty list in the controller but have not set the account.
At what level in the controller should i be setting the account?
I have tried it at the sections below
Should it be  public List<Account> returns {get;set;} 
Account acc=new Account();

Any help would be much appreciated.

Thanks

Full Code:

VF:
<apex:page controller="OnlineLoanAppLoginController" showHeader="false" sidebar="false" standardStylesheets="true">
<apex:Pagemessages id="msg"/>
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection title="Loan Application Login">
<apex:panelGrid columns="2" style="margin-top:1em;">
<p><b>UserName</b><br />
    <apex:inputText value="{!Username}"/>
</p>
<p><b>Password</b><br />
<apex:inputSecret value="{!password}"/>
</p>
  <apex:commandButton action="{!OnlineLoanAppLogin}" value="Login" reRender="Error"/>
</apex:panelGrid>
</apex:pageBlockSection>
</apex:pageBlock>                                                                          
</apex:form>
</apex:page>


Controller:
public class OnlineLoanAppLoginController {
    public String userName{get;set;}
    public String password{get;set;}
    public List<Account> returns {get;set;}
    public OnlineLoanAppLoginController(){
        returns =new List<Account>();
    }
    public PageReference OnlineLoanAppLogin(){
        
        Account acc=new Account();
        Acc= [select ID, Name, Loan_Application_Username__pc, Loan_Application_Password__pc, sic from Account Where Loan_Application_Username__pc=:acc.Loan_Application_Username__pc and Loan_Application_Password__pc=:acc.Loan_Application_Password__pc];
        if(userName == acc.Loan_Application_Username__pc && password == acc.Loan_Application_Password__pc){
            Pagereference pr = new PageReference('Apex/OnlineLoanApplication'+returns[0].Id);
            pr.setReDirect(true);
            return pr;
        }
        
        else if (userName != acc.Loan_Application_Username__pc&& Password == acc.Loan_Application_Password__pc){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter valid User Name'));
            return null;

        }
        else if ( userName == acc.Loan_Application_Username__pc  && Password != acc.Loan_Application_Password__pc){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Password is Incorrect'));
            return null;
        }
        
        else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid UserName or Password'));
                return null;
        }
        
    }

}