api for initial widgets

This commit is contained in:
Gabe Farrell 2023-04-10 22:39:51 -04:00
parent 36f751a69f
commit 4aa8a2f822
14 changed files with 864 additions and 11 deletions

View file

@ -18,6 +18,7 @@ import (
)
func CreateAccount(w http.ResponseWriter, r *http.Request) {
log.Println("* /auth/createaccount")
// prepare DB
err := db.Client.Ping(context.Background(), readpref.Primary())
if err != nil {
@ -26,7 +27,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
// var v contains POST credentials
var v UserSchema
var v db.UserSchema
r.ParseForm()
v.Email = r.FormValue("email")
v.Password = r.FormValue("password")
@ -72,6 +73,8 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
// add the new user to the database
v.Session = sessionID
v.Password = string(hashedPass)
v.Balance.Currency = "USD"
v.Budget.Currency = "USD"
_, err = userCollection.InsertOne(r.Context(), v)
if err != nil {
log.Println("* Error inserting new user")

View file

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
@ -16,13 +17,6 @@ import (
"golang.org/x/crypto/bcrypt"
)
type UserSchema struct {
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Session string `json:"session" bson:"session"`
}
type LoginResponse struct {
Name string `json:"name"`
Email string `json:"email"`
@ -30,6 +24,7 @@ type LoginResponse struct {
}
func Login(w http.ResponseWriter, r *http.Request) {
log.Println("* /auth/login")
// prepare DB
err := db.Client.Ping(context.Background(), readpref.Primary())
if err != nil {
@ -38,7 +33,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
// var v contains POST credentials
var v UserSchema
var v db.UserSchema
r.ParseForm()
v.Email = r.FormValue("email")
v.Password = r.FormValue("password")
@ -48,7 +43,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
}
// cmp struct will be compared with v to verify credentials
var cmp UserSchema
var cmp db.UserSchema
found := userCollection.FindOne(r.Context(), bson.D{primitive.E{Key: "email", Value: strings.ToLower(v.Email)}})
if found.Err() != nil {

55
routes/userInfo.go Normal file
View file

@ -0,0 +1,55 @@
package routes
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/jacobmveber-01839764/BudgetBuddy/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type UserInfoResponse struct {
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
}
func UserInfo(w http.ResponseWriter, r *http.Request) {
// get session key from request
session := r.Header.Get("x-session-key")
// get collection handle from db
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
found := userCollection.FindOne(r.Context(), bson.D{primitive.E{Key: "session", Value: strings.ToLower(session)}})
if found.Err() != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\":\"session key invalid\"}")
return
}
var user = db.UserSchema{}
err := found.Decode(&user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"problem decoding user\"}")
return
}
info := UserInfoResponse{
Name: user.Name,
Email: user.Email,
}
ret, err := json.Marshal(info)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"problem marshalling response\"}")
return
}
w.Write(ret)
}