Skip to content

Instantly share code, notes, and snippets.

@newtoallofthis123
Created January 12, 2024 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newtoallofthis123/89b8dd2ed52a5a68d17309dccd72cfb3 to your computer and use it in GitHub Desktop.
Save newtoallofthis123/89b8dd2ed52a5a68d17309dccd72cfb3 to your computer and use it in GitHub Desktop.
User Password Auth
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt"
)
func main() {
// Replace the connStr with a something like this
// const connStr = "user=postgres password=postgres host=localhost dbname=yt port=5432 sslmode=disable"
dbConn, err := sql.Open("postgres", connStr)
if err != nil {
panic(err)
}
query := "SELECT password from users WHERE username = $1"
var password string
err = dbConn.QueryRow(query, "noob").Scan(&password)
if err != nil {
panic(err)
}
fmt.Println("The Correct Password is:", password)
// scan password from the user
userPassword := os.Args[1]
fmt.Println("The user entered", userPassword)
if bcrypt.CompareHashAndPassword([]byte(password), []byte(userPassword)) == nil {
fmt.Println("You are logged in")
} else {
fmt.Println("You are not logged in")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment