diff --git a/assets/src/pages/register.tsx b/assets/src/pages/register.tsx
index ff2d316..0ead1d0 100644
--- a/assets/src/pages/register.tsx
+++ b/assets/src/pages/register.tsx
@@ -1,9 +1,10 @@
import React, { useCallback, useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";
-import { useAuthUser } from "../providers/AuthUser";
+import { useAppDispatch } from "../app/hooks";
+import { register } from "../features/auth/auth-slice";
const Register: React.FC = () => {
- const { register } = useAuthUser();
+ const dispatch = useAppDispatch();
const navigate = useNavigate();
const [username, setUsername] = useState("");
@@ -16,9 +17,7 @@ const Register: React.FC = () => {
return;
}
- register({ email, username, password }).then((res) => {
- res ? navigate("/app") : alert("Someting went wrong");
- });
+ dispatch(register({ email, username, password }));
};
return (
diff --git a/assets/src/providers/AuthUser.tsx b/assets/src/providers/AuthUser.tsx
deleted file mode 100644
index 896372b..0000000
--- a/assets/src/providers/AuthUser.tsx
+++ /dev/null
@@ -1,95 +0,0 @@
-import {
- createContext,
- useCallback,
- useContext,
- useEffect,
- useState,
-} from "react";
-import axios from "axios";
-import User from "../models/User";
-
-interface IAuthUserContext {
- authenticated: boolean;
- user?: User | undefined;
- login: (user: AuthCredentials) => Promise
;
- logout: () => Promise;
- register: (user: RegisterCredentials) => Promise;
-}
-const AuthUserContext = createContext(null);
-
-export interface AuthCredentials {
- username: string;
- password: string;
-}
-
-export interface RegisterCredentials extends AuthCredentials {
- email: string;
-}
-
-export const AuthUserProvider: React.FC = ({ children }) => {
- const [loading, setLoading] = useState(true);
- const [authenticated, setAuthenticated] = useState(false);
- const [user, setUser] = useState();
- const login = useCallback(async (user: AuthCredentials) => {
- try {
- const res = await axios.post("/api/auth/login", user);
- setUser(res.data);
- setAuthenticated(true);
- return true;
- } catch {
- setAuthenticated(false);
- return false;
- }
- }, []);
- const logout = useCallback(async () => {
- await axios.get("/api/auth/logout");
- setAuthenticated(false);
- setUser(undefined);
- }, []);
-
- const register = useCallback(async (user: RegisterCredentials) => {
- try {
- const res = await axios.post("/api/auth/register", user);
- setUser(res.data);
- setAuthenticated(true);
- return true;
- } catch {
- return false;
- }
- }, []);
-
- useEffect(() => {
- const verify = async () => {
- try {
- const res = await axios.get("/api/auth/verify");
- setUser(res.data);
- setAuthenticated(true);
- } catch {
- setAuthenticated(false);
- setUser(undefined);
- } finally {
- setLoading(false);
- }
- };
- verify();
- }, []);
-
- return loading ? (
- Loading...
- ) : (
-
- {children}
-
- );
-};
-
-export const useAuthUser = () => {
- const context = useContext(AuthUserContext);
-
- if (!context)
- throw new Error("useAuthUser can only be used inside AuthContextProvider");
-
- return context;
-};
diff --git a/assets/src/styles/GlobalStyles.ts b/assets/src/styles/GlobalStyles.ts
index 5e1f346..353e491 100644
--- a/assets/src/styles/GlobalStyles.ts
+++ b/assets/src/styles/GlobalStyles.ts
@@ -16,6 +16,7 @@ export const GlobalStyles = createGlobalStyle`
width: 100%;
height: 100%;
max-height: calc(100% + 0px);
+ overflow:hidden;
}
#root {
width: 100%;
@@ -68,4 +69,4 @@ export const GlobalStyles = createGlobalStyle`
}
`;
-export default GlobalStyles;
\ No newline at end of file
+export default GlobalStyles;
diff --git a/assets/src/styles/Variables.ts b/assets/src/styles/Variables.ts
index f495df0..7a5666d 100644
--- a/assets/src/styles/Variables.ts
+++ b/assets/src/styles/Variables.ts
@@ -11,6 +11,7 @@ const Variables = css`
--background: white;
--foreground: black;
+ --card-size: 300px;
}
`;
diff --git a/auth/userscope.go b/auth/userscope.go
index 34532b9..a1f168a 100644
--- a/auth/userscope.go
+++ b/auth/userscope.go
@@ -6,8 +6,8 @@ import (
)
func UserScope(c *gin.Context) func(db *gorm.DB) *gorm.DB {
+ userId := int(c.GetFloat64("user_id"))
return func(db *gorm.DB) *gorm.DB {
- userId := c.GetUint("user_id")
return db.Where("user_id", userId)
}
}
diff --git a/controllers/v1/card/card.controller.go b/controllers/v1/card/card.controller.go
index 38db7e9..94db5cb 100644
--- a/controllers/v1/card/card.controller.go
+++ b/controllers/v1/card/card.controller.go
@@ -26,16 +26,17 @@ func getCardById(c *gin.Context) {
}
func createCard(c *gin.Context) {
- var card models.Card
+ var card models.CardDto
if err := c.BindJSON(&card); err != nil {
return
}
- card.UserID = c.GetUint("user_id")
+ cardDbo := card.ToDbo()
+ cardDbo.UserID = uint(c.GetFloat64("user_id"))
- if models.DB.Create(&card).Save(&card).Error != nil {
+ if models.DB.Create(&cardDbo).Save(&cardDbo).Error != nil {
return
}
- c.IndentedJSON(http.StatusCreated, card.ToDto())
+ c.IndentedJSON(http.StatusCreated, cardDbo.ToDto())
}
func updateCard(c *gin.Context) {
diff --git a/controllers/v1/carddeck/carddeck.controller.go b/controllers/v1/carddeck/carddeck.controller.go
index 05b5b98..ea77a2e 100644
--- a/controllers/v1/carddeck/carddeck.controller.go
+++ b/controllers/v1/carddeck/carddeck.controller.go
@@ -40,7 +40,7 @@ func createCardDeck(c *gin.Context) {
if err := c.BindJSON(&cardDeck); err != nil {
return
}
- cardDeck.UserID = c.GetUint("user_id")
+ cardDeck.UserID = uint(c.GetFloat64("user_id"))
if models.DB.Scopes(auth.UserScope(c)).Create(&cardDeck).Save(&cardDeck).Error != nil {
return
diff --git a/models/card.go b/models/card.go
index 418cf89..4e69160 100644
--- a/models/card.go
+++ b/models/card.go
@@ -31,6 +31,9 @@ type CardDto struct {
func (c Card) ToDto() CardDto {
return CardDto{c.ID, c.Front, c.Back, c.Hint, c.CardDeckID}
}
+func (c CardDto) ToDbo() Card {
+ return Card{Front: c.Front, Back: c.Back, Hint: c.Hint, CardDeckID: c.CardDeckID}
+}
func (c *Card) BeforeCreate(tx *gorm.DB) (err error) {
c.PhaseID = phaseOneID