module Main where

import Network.CGI
import Data.Maybe

import DB
import Session
import LoginPage
import FancyHtml

main = runCGI $ handleErrors cgiMain

cgiMain = do
    username <- getInput "username"
    password <- getInput "password"
    let pass = fromJust password
    let user = fromJust username
    res <- liftIO $ db_select $ "SELECT * FROM users WHERE username LIKE '" ++ user ++ "' AND password LIKE '" ++ pass ++ "'"
    if (length res > 0)
        then do
            sessId <- liftIO $ new_session_id user
            output $ loggedInPage sessId
        else output $ header ++ errorBox "Incorrect username or password." ++ footer
