Created
January 12, 2024 12:10
-
-
Save newtoallofthis123/89b8dd2ed52a5a68d17309dccd72cfb3 to your computer and use it in GitHub Desktop.
User Password Auth
This file contains 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 ( | |
"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