Created
December 1, 2025 11:00
-
-
Save aminnairi/c4f7a41a7f4244fecc59da992ad7fe80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/rand" | |
| "database/sql" | |
| "encoding/base64" | |
| "fmt" | |
| "log" | |
| "golang.org/x/crypto/bcrypt" | |
| _ "modernc.org/sqlite" | |
| ) | |
| type User struct { | |
| Email string | |
| Password string | |
| } | |
| func main() { | |
| database, openError := sql.Open("sqlite", "./database.sqlite") | |
| if openError != nil { | |
| log.Fatal("Error while opening the database:", openError) | |
| } | |
| // Intialise la base de données avec le modèle utilisateur | |
| initializeDataModel(database) | |
| email := "[email protected]" | |
| password := "password" | |
| selectStatement, prepareError := database.Prepare("SELECT email, password FROM users WHERE email = '[email protected]';") | |
| if prepareError != nil { | |
| log.Fatal("Error while selecting user:", prepareError) | |
| } | |
| rows, queryError := selectStatement.Query(email) | |
| if queryError != nil { | |
| log.Fatal("Error while querying user:", queryError) | |
| } | |
| user := User{} | |
| fmt.Println("Executing request here") | |
| if !rows.Next() { | |
| log.Fatal("Aucun utilisateur ne correspond à l'email") | |
| } | |
| scanError := rows.Scan(&user.Email, &user.Password) | |
| if scanError != nil { | |
| log.Fatal("Error while scanning values:", scanError) | |
| } | |
| compareError := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) | |
| if compareError != nil { | |
| log.Fatal("Error, password mismatch") | |
| } | |
| // 4. Création d'un jeton de connexion (authentication token) | |
| bytes := make([]byte, 32) | |
| _, readError := rand.Read(bytes) | |
| if readError != nil { | |
| log.Fatal("Unable to read random:", readError) | |
| } | |
| token := base64.RawURLEncoding.EncodeToString(bytes) | |
| fmt.Println("Authentication token is:", token) | |
| } | |
| func initializeDataModel(database *sql.DB) { | |
| _, execError := database.Exec("CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, email VARCHAR(50) NOT NULL UNIQUE, password CHAR(60) NOT NULL)") | |
| if execError != nil { | |
| log.Fatal("Error while creating the table:", execError) | |
| } | |
| insertStatement, prepareError := database.Prepare("INSERT INTO users(email, password) VALUES(?, ?);") | |
| if prepareError != nil { | |
| log.Fatal("Error preparing insertion of user:", prepareError) | |
| } | |
| bytes, bcryptError := bcrypt.GenerateFromPassword([]byte("password"), 10) | |
| if bcryptError != nil { | |
| log.Fatal("Error while generating a password:", bcryptError) | |
| } | |
| user := User{ | |
| Email: "[email protected]", | |
| Password: string(bytes), | |
| } | |
| _, insertError := insertStatement.Exec(user.Email, user.Password) | |
| if insertError != nil { | |
| log.Fatal("Error while inserting a user:", insertError) | |
| } | |
| fmt.Println("Successfully created table with one user") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment