Just a simple Loginengine

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Posts: 103
Joined: Feb 11, 2013 12:23

Just a simple Loginengine

Post by mrminecrafttnt »

Yay, i think its a very nice Loginengine :)

Code: Select all

type rightstable
    admin_rights as integer
    user_rights as integer
    is_banned as integer
end type

type account
    dim as string password
    dim as string username
    dim as integer id
    dim as rightstable rights
    dim as integer isloggedin
end type

dim as account accounts(10)
dim shared as account nothing
dim shared as account mainaccount
with accounts(0)
    .username = "root"
    .password = "Swordfish"
    .id = 1
    .rights.admin_rights = 1
    .rights.user_rights = 1
end with

with accounts(1)
    .username = "user"
    .password = "user"
    .id = 2
    .rights.admin_rights = 0
    .rights.user_rights = 1
end with

function search_account_by_username (username as string,accounts() as account) as integer
    for i as integer = lbound(accounts) to ubound(accounts)
        if username = accounts(i).username and username <> "" then return i
    return -1
end function

function login(username as string,password as string,accounts() as account) as integer
    dim as integer account_tbl_id = search_account_by_username (username,accounts()) 
    if account_tbl_id > -1 then
        if username = accounts(account_tbl_id).username and password = accounts(account_tbl_id).password then
            if mainaccount.rights.is_banned = 1 then return 0
            mainaccount = accounts(account_tbl_id)
            mainaccount.isloggedin = 1
            return 1
            return 0
        end if
        return 0
    end if
end function

sub logout
    mainaccount = nothing
    Print "Logout sucessful"
end sub

'Example here
dim as string username,password
print "** LOGIN **"
Input "Username: ",Username
Input "Password: ", Password
dim as integer status = login(username,password,accounts())
if status = 1 then 
    print "Welcome ";mainaccount.username
    print "Login failed"
end if

Post Reply