feat: add admin auth middleware
This commit is contained in:
@@ -16,10 +16,14 @@ type AuthBody struct {
|
|||||||
Password string `json:"password" validate:"required"`
|
Password string `json:"password" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TokenResponse struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
|
||||||
// Login
|
// Login
|
||||||
// @Router /v1/admin/auth [post]
|
// @Router /v1/admin/auth [post]
|
||||||
// @Bind body body
|
// @Bind body body
|
||||||
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (string, error) {
|
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
|
||||||
if body.Username == "admin" && body.Password == "xixi@0202" {
|
if body.Username == "admin" && body.Password == "xixi@0202" {
|
||||||
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||||
UserID: 1,
|
UserID: 1,
|
||||||
@@ -27,9 +31,9 @@ func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (string, error) {
|
|||||||
|
|
||||||
token, err := ctl.jwt.CreateToken(claim)
|
token, err := ctl.jwt.CreateToken(claim)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
return token, nil
|
return &TokenResponse{Token: token}, nil
|
||||||
}
|
}
|
||||||
return "", fiber.ErrUnauthorized
|
return nil, fiber.ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,18 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(ctx.Path(), "/v1/admin/") {
|
if strings.HasPrefix(ctx.Path(), "/v1/admin/") {
|
||||||
|
token := ctx.Get("Authorization")
|
||||||
|
if token == "" {
|
||||||
|
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
||||||
|
}
|
||||||
|
jwt, err := f.jwt.Parse(token)
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
||||||
|
}
|
||||||
|
if jwt.UserID != 1 {
|
||||||
|
return ctx.Status(fiber.StatusForbidden).SendString("Forbidden")
|
||||||
|
}
|
||||||
|
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useAuthStore } from '@/stores/auth';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
// Create axios instance with default config
|
// Create axios instance with default config
|
||||||
@@ -12,11 +13,10 @@ const httpClient = axios.create({
|
|||||||
// Request interceptor
|
// Request interceptor
|
||||||
httpClient.interceptors.request.use(
|
httpClient.interceptors.request.use(
|
||||||
config => {
|
config => {
|
||||||
// You can add auth token here if needed
|
const authStore = useAuthStore();
|
||||||
// const token = localStorage.getItem('token');
|
if (authStore.isAuthenticated && authStore.token) {
|
||||||
// if (token) {
|
config.headers.Authorization = `Bearer ${authStore.token}`;
|
||||||
// config.headers.Authorization = `Bearer ${token}`;
|
}
|
||||||
// }
|
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ const validateForm = () => {
|
|||||||
errorMessage.value = '请输入密码';
|
errorMessage.value = '请输入密码';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (password.value.length < 8) {
|
||||||
|
errorMessage.value = '密码至少需要8个字符';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,7 +39,8 @@ const handleLogin = async () => {
|
|||||||
errorMessage.value = '';
|
errorMessage.value = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const token = await authService.login(username.value, password.value);
|
const resp = await authService.login(username.value, password.value);
|
||||||
|
const { token } = resp.data;
|
||||||
authStore.setToken(token);
|
authStore.setToken(token);
|
||||||
router.push('/');
|
router.push('/');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -2,21 +2,21 @@ import { defineStore } from 'pinia';
|
|||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
const token = ref(localStorage.getItem('token'));
|
const token = ref(localStorage.getItem('__token'));
|
||||||
const user = ref(null);
|
const user = ref(null);
|
||||||
|
|
||||||
const isAuthenticated = computed(() => !!token.value);
|
const isAuthenticated = computed(() => !!token.value);
|
||||||
|
|
||||||
function setToken(newToken) {
|
function setToken(newToken) {
|
||||||
token.value = newToken;
|
token.value = newToken;
|
||||||
localStorage.setItem('token', newToken);
|
localStorage.setItem('__token', newToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
token.value = null;
|
token.value = null;
|
||||||
user.value = null;
|
user.value = null;
|
||||||
localStorage.removeItem('token');
|
localStorage.removeItem('__token');
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user