コード例 #1
0
ファイル: user_helper.go プロジェクト: gabz75/env-auth-go
// GetUserByToken -
func GetUserByToken(token string) (*User, error) {
	db := orm.DatabaseConnection()

	query := "SELECT users.* FROM users INNER JOIN sessions ON sessions.user_id = users.id WHERE sessions.token = $1"
	rows, err := db.Query(query, token)

	if err != nil {
		panic(err)
	}

	defer rows.Close()

	var user User
	var id int64
	var userEmail string
	var hashedPassword string

	for rows.Next() {
		err = rows.Scan(&id, &userEmail, &hashedPassword)
		if err != nil {
			panic(err)
		}
		user = User{ID: id, Email: userEmail, Password: hashedPassword}
	}

	if user.ID == 0 {
		return nil, errors.New("invalid session")
	}

	return &user, nil
}
コード例 #2
0
ファイル: user_helper.go プロジェクト: gabz75/env-auth-go
// GetUserByEmailAndPassword fetch user by email
func GetUserByEmailAndPassword(email string, password string) (*User, error) {
	db := orm.DatabaseConnection()

	rows, err := db.Query("SELECT * FROM users WHERE email = $1", email)

	if err != nil {
		panic(err)
	}

	defer rows.Close()

	var user User
	var id int64
	var userEmail string
	var hashedPassword string

	for rows.Next() {
		err = rows.Scan(&id, &userEmail, &hashedPassword)
		if err != nil {
			panic(err)
		}
		user = User{ID: id, Email: userEmail, Password: hashedPassword}
	}

	nomatch := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))

	if nomatch != nil || user.ID == 0 {
		return nil, errors.New("invalid email/password")
	}

	return &user, nil
}
コード例 #3
0
ファイル: session_helper.go プロジェクト: gabz75/env-auth-go
// GetSession -
func GetSession(user User, token string) (*Session, error) {
	db := orm.DatabaseConnection()

	rows, err := db.Query("SELECT * FROM sessions WHERE user_id = $1 AND token = $2", user.ID, token)

	if err != nil {
		panic(err)
	}

	defer rows.Close()

	var session Session
	var id int64
	var userID int64
	var sessionToken string

	for rows.Next() {
		err = rows.Scan(&id, &userID, &sessionToken)
		if err != nil {
			panic(err)
		}
		session = Session{ID: id, UserID: userID, Token: sessionToken}
	}

	if session.ID == 0 {
		return nil, errors.New("no session found")
	}

	return &session, nil
}
コード例 #4
0
ファイル: user_helper.go プロジェクト: gabz75/env-auth-go
// AvailableEmail -
func AvailableEmail(email string) bool {
	db := orm.DatabaseConnection()

	rows, err := db.Query("SELECT count(*) FROM users WHERE email = $1", email)

	if err != nil {
		panic(err)
	}

	defer rows.Close()

	var count int64

	for rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			panic(err)
		}
	}

	return count == 0
}
コード例 #5
0
ファイル: session_helper.go プロジェクト: gabz75/env-auth-go
// GetSessions -
func GetSessions(user User) []Session {
	db := orm.DatabaseConnection()

	rows, err := db.Query("SELECT * FROM sessions WHERE user_id = $1", user.ID)

	defer rows.Close()

	var sessions []Session
	var id int64
	var userID int64
	var token string

	for rows.Next() {
		err = rows.Scan(&id, &userID, &token)
		if err != nil {
			panic(err)
		}
		sessions = append(sessions, Session{ID: id, UserID: userID, Token: token})
	}

	return sessions
}