0

I really want to create login code in batch file that can authenticate users credentials, but by trying to google some stuff to make it work but i still can't, so i would like to ask some of professional coders, developers and programmers to help me and teach me about my mistakes since i'm still a student and newbie about programming.

Here's my code which is not authenticating after i created a new user and login in.

Please teach me well :<

@echo off
SETLOCAL EnableDelayedExpansion

MODE con: cols=51 lines=18
goto login

::Registration 
    :reg
        cls
        echo Create Account
        echo.
        set /p "uname=Username:"
        set /p "pass=Password:"
        
        echo Username: %uname% >> C:\Test\users.txt
        echo Password: %pass% >> C:\Test\users.txt

        goto login
        
::Login
    :login
        cls
        echo Please Login. To create new account type "new".
        echo.
        set /p "uname=Username:"
        if "%uname%"=="new" goto reg
        set /p "pass=Password:"
        
        call:authenticate
            
        goto succ
        
::Error
    :errorlogin
        cls
        echo Invalid credentials.
        pause >nul
        goto login
        
::Successful Login
    :succ
        cls 
        echo Login Successful
        pause >nul
        exit
        
::Copying/Backupping file and moving file after successful login
    :copy
        copy "C:\Users\userfile.sav" - "D:\Backup\"
        copy "C:\Users\userfile.sett" - "D:\Backup\"
        
        move "F:\unorthodox.exe" - "C:\Users\"
        
        goto done
        
::final
    :done
        echo     OOOOO    OOOO   O     O  OOOOO   O
        echo     O    O  O    O  O O   O  O       O
        echo     O    O  O    O  O  O  O  OOO     O
        echo     O    O  O    O  O    OO  O
        echo     OOOOO    OOOO   O     O  OOOOOO  O
        echo.
        echo     S  U  C  C  E  S  S  F  U  L  L  Y
        
        pause >nul
        
::====================================::
:authenticate
for /f "tokens=*" %%a in (users.txt) do (
    if {%%a}=={%uname%}
    if {%%a}=={%pass%} )
    
    else
    goto errorlogin

and i would like to mask the password with asterisk so that it's more like secure, also i would like to add loading screen indicating about copy/moving progress.

so much very thank you in advance.

Juniorzkie
  • 19
  • 7
  • 1
    Are you having issues with the current code, or are you requesting additional code to compliment your current code? – Gerhard Jul 06 '20 at 13:52
  • Having an issue and requesting additional code for my current code – Juniorzkie Jul 06 '20 at 13:53
  • i'm having a trouble about logging in authentication with my existing user input. – Juniorzkie Jul 06 '20 at 14:03
  • I saw that part yes. let me have a look. – Gerhard Jul 06 '20 at 14:03
  • Take a look here ==> [Login and Register system in Batch File](https://stackoverflow.com/questions/44608074/login-and-register-system-in-batch-file?answertab=active#tab-top) – Hackoo Jul 06 '20 at 14:39
  • that's where did i get the idea of `if "%uname%"=="new" goto reg ` code. @Hackoo – Juniorzkie Jul 06 '20 at 14:48
  • echo user and password to one line `echo %uname% %pass% >> users.txt` then verify the user and password by searching for the string `for /f "tokens=1*" %%a in ('type users.txt ^| findstr /i "%uname%"`).. ` you can use `%%a` to verify username and `%%b` to verify password. – Gerhard Jul 06 '20 at 14:53
  • it's still not working sir @GerhardBarnard this is what i did `for /f "tokens=1" %%a in (users.txt ^| findstr /i "%uname%") do ( if {%%a}=={%uname%} if {%%b}=={%pass%} ) else goto errorlogin ' _i also tried the_ for /f "tokens=1*" %%a in ('type users.txt ^| findstr /i "%uname%") do ( if {%%a}=={%uname%} if {%%b}=={%pass%} ) – Juniorzkie Jul 06 '20 at 15:02

1 Answers1

1

Here is a working model of your code with some additional checks such as verifying a user does not already exist and ensure the user confirms password:

I would suggest you have a look at what rhe new layout is of your users.txt file after you create a user. So it would be best to clear the file and restart a new one break>c:\test\users.txt

@echo off
SETLOCAL EnableDelayedExpansion

MODE con: cols=51 lines=18
echo(
echo   ---------------------------------
echo ^| 1. Logon Existing Existing User^|
echo ^| 2. Create New User             ^|
echo   ---------------------------------
echo(
choice /C 12 /M "Please select: "
if %errorlevel% equ 1 goto login

::Registration 
    :reg
        cls
        echo Create User.
        echo(
        set /p "uname=Username: "
        for /f "usebackq delims=" %%a in (`type "c:\test\users.txt" ^| findstr /i "'%uname%''"`) do (
            if %errorlevel% equ 0 (
               echo User already exists. Please enter new user. Press enter to retry.
               pause>nul
               goto reg:
        )
     )
        :setpass
        set /p "pass=Password: "
        set /p "conf=Confirm Password: "
        if not "%pass%" == "%conf%" (
          echo passwords do not match, please try again. Press enter to retry.
          pause>nul
          goto setpass
        )
        echo '%uname%''%pass%'>>"c:\test\users.txt"
        goto login
        
::Login
    :login
        cls
        echo Please Login.
        echo(
        set /p "uname=Username: "
        set /p "pass=Password: "
        
        goto authenticate
        
::Error
    :errorlogin
        cls
        echo Invalid credentials.
        pause >nul
        goto login
        
::Successful Login
    :succ
        cls 
        echo Login Successful
        pause >nul
        exit
        
::Copying/Backupping file and moving file after successful login
    :copy
        copy "C:\Users\userfile.sav" - "D:\Backup\"
        copy "C:\Users\userfile.sett" - "D:\Backup\"
        
        move "F:\unorthodox.exe" - "C:\Users\"
        
        goto done
        
::final
    :done
        echo     OOOOO    OOOO   O     O  OOOOO   O
        echo     O    O  O    O  O O   O  O       O
        echo     O    O  O    O  O  O  O  OOO     O
        echo     O    O  O    O  O    OO  O
        echo     OOOOO    OOOO   O     O  OOOOOO  O
        echo.
        echo     S  U  C  C  E  S  S  F  U  L  L  Y
        
        pause >nul
        
::====================================::
:authenticate
for /f "usebackq delims=" %%a in (`type "c:\test\users.txt" ^| findstr "'%uname%''%pass%'"`) do (
    if "%%a"=="'%uname%''%pass%'" (
       goto succ
   )
)
goto errorlogin
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • it always says successful login even though i didn't type any password :< so, i removed the `goto succ ` and still not working. – Juniorzkie Jul 07 '20 at 07:44
  • It's WORKING! Thank you sir! now i will try to modify something in copy section. – Juniorzkie Jul 08 '20 at 03:56
  • sir? can i ask how can i hide or mask the password while typing it? – Juniorzkie Oct 13 '20 at 06:56
  • have a look at the [answer here](https://stackoverflow.com/questions/36291324/batch-file-command-hide-password/36292638) by SomethingDark. You can copy that code as is and make a few adjustments to your current code to let it call the password mask. – Gerhard Oct 13 '20 at 07:46
  • 1
    Thank you veru much sir! you really helped me a lot. – Juniorzkie Oct 14 '20 at 08:00