Michael Gold
Michael Gold

Reputation: 41

IHP Script to create a new user

I’m trying out IHP for a new application, where we lock everything behind authentication and only allow existing users to sign up other new users… what is the best way to seed a user, or at least create a user outside of the application? Can a Script take user input? It looks like it doesn’t wait for input from the user (and requires a redirect from stdin, which isn’t the worst thing in the world), and doesn’t take arguments, so I’m wondering if there’s an alternative?

Upvotes: 2

Views: 67

Answers (2)

Varun Rajput
Varun Rajput

Reputation: 270

You can use this if you want to create a user account using IHP Script:

run :: Script
run = do
    let username = "admin"
        password = "admin"
    
    hashed <- hashPassword password

    existingUser <- query @User 
                |> findMaybeBy #email username

    user <- case existingUser of
        Just user -> do
            putStrLn $ username ++ " already existing, skipping..."
            return user
        _ -> do
            putStrLn $ "Inserting " ++ username ++ "..."
            hashed <- hashPassword password
            
            user <- newRecord @User
                |> set #email username
                |> set #passwordHash hashed
                |> createRecord
            putStrLn $ "Inserted " ++ username
            return user
    
    return ()

You can easily change the default values above to accept an input from the user but yes, as Micheal mentioned above, IHP Scripts aren't designed to handle user inputs and using the IHP IDE would be better to create user.

Upvotes: 1

Michael Gold
Michael Gold

Reputation: 41

By design, IHP scripts do not allow for user input.

It was suggested to insert into the database directly to initially seed a user.

We can use hash-password from the command line to generate a hash and insert into the database.

I was able to use the IHP web IDE to execute the INSERT statement successfully.

Another options is something like echo "INSERT INTO users (..) VALUES (..)" | psql "$DATABASE_URL" directly

Upvotes: 2

Related Questions