feat: add admin login
This commit is contained in:
35
backend/app/http/admin/auth.go
Normal file
35
backend/app/http/admin/auth.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"quyun/providers/jwt"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// @provider
|
||||||
|
type auth struct {
|
||||||
|
jwt *jwt.JWT
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthBody struct {
|
||||||
|
Username string `json:"username" validate:"required"`
|
||||||
|
Password string `json:"password" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login
|
||||||
|
// @Router /v1/admin/auth [post]
|
||||||
|
// @Bind body body
|
||||||
|
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (string, error) {
|
||||||
|
if body.Username == "admin" && body.Password == "xixi@0202" {
|
||||||
|
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||||
|
UserID: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
token, err := ctl.jwt.CreateToken(claim)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
return "", fiber.ErrUnauthorized
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package admin
|
|||||||
import (
|
import (
|
||||||
"quyun/providers/ali"
|
"quyun/providers/ali"
|
||||||
"quyun/providers/app"
|
"quyun/providers/app"
|
||||||
|
"quyun/providers/jwt"
|
||||||
|
|
||||||
"go.ipao.vip/atom"
|
"go.ipao.vip/atom"
|
||||||
"go.ipao.vip/atom/container"
|
"go.ipao.vip/atom/container"
|
||||||
@@ -11,6 +12,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Provide(opts ...opt.Option) error {
|
func Provide(opts ...opt.Option) error {
|
||||||
|
if err := container.Container.Provide(func(
|
||||||
|
jwt *jwt.JWT,
|
||||||
|
) (*auth, error) {
|
||||||
|
obj := &auth{
|
||||||
|
jwt: jwt,
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj, nil
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := container.Container.Provide(func() (*medias, error) {
|
if err := container.Container.Provide(func() (*medias, error) {
|
||||||
obj := &medias{}
|
obj := &medias{}
|
||||||
|
|
||||||
@@ -33,6 +45,7 @@ func Provide(opts ...opt.Option) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
|
auth *auth,
|
||||||
medias *medias,
|
medias *medias,
|
||||||
orders *orders,
|
orders *orders,
|
||||||
posts *posts,
|
posts *posts,
|
||||||
@@ -40,6 +53,7 @@ func Provide(opts ...opt.Option) error {
|
|||||||
users *users,
|
users *users,
|
||||||
) (contracts.HttpRoute, error) {
|
) (contracts.HttpRoute, error) {
|
||||||
obj := &Routes{
|
obj := &Routes{
|
||||||
|
auth: auth,
|
||||||
medias: medias,
|
medias: medias,
|
||||||
orders: orders,
|
orders: orders,
|
||||||
posts: posts,
|
posts: posts,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||||
type Routes struct {
|
type Routes struct {
|
||||||
log *log.Entry `inject:"false"`
|
log *log.Entry `inject:"false"`
|
||||||
|
auth *auth
|
||||||
medias *medias
|
medias *medias
|
||||||
orders *orders
|
orders *orders
|
||||||
posts *posts
|
posts *posts
|
||||||
@@ -32,6 +33,12 @@ func (r *Routes) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Routes) Register(router fiber.Router) {
|
func (r *Routes) Register(router fiber.Router) {
|
||||||
|
// 注册路由组: auth
|
||||||
|
router.Post("/v1/admin/auth", DataFunc1(
|
||||||
|
r.auth.Login,
|
||||||
|
Body[AuthBody]("body"),
|
||||||
|
))
|
||||||
|
|
||||||
// 注册路由组: medias
|
// 注册路由组: medias
|
||||||
router.Get("/v1/admin/medias", DataFunc2(
|
router.Get("/v1/admin/medias", DataFunc2(
|
||||||
r.medias.List,
|
r.medias.List,
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
||||||
if strings.HasPrefix(ctx.Path(), "/admin/") {
|
if ctx.Path() == "/v1/admin/auth" {
|
||||||
|
return ctx.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(ctx.Path(), "/v1/admin/") {
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { http } from '@/utils/http';
|
import httpClient from './httpClient';
|
||||||
|
|
||||||
export const authService = {
|
export const authService = {
|
||||||
login(username, password) {
|
login(username, password) {
|
||||||
return http.post('/admin/auth/login', { username, password });
|
return httpClient.post('/admin/auth', { username, password });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
|
import { authService } from '@/api/authService';
|
||||||
import { useAuthStore } from '@/stores/auth';
|
import { useAuthStore } from '@/stores/auth';
|
||||||
import Button from 'primevue/button';
|
import Button from 'primevue/button';
|
||||||
import Card from 'primevue/card';
|
import Card from 'primevue/card';
|
||||||
@@ -10,17 +11,32 @@ import { useRouter } from 'vue-router';
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const authStore = useAuthStore();
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
const username = ref('');
|
const username = ref('admin');
|
||||||
const password = ref('');
|
const password = ref('xixi@0202');
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const errorMessage = ref('');
|
const errorMessage = ref('');
|
||||||
|
|
||||||
|
const validateForm = () => {
|
||||||
|
if (!username.value.trim()) {
|
||||||
|
errorMessage.value = '请输入用户名';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!password.value) {
|
||||||
|
errorMessage.value = '请输入密码';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
|
if (!validateForm()) return;
|
||||||
|
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
errorMessage.value = '';
|
errorMessage.value = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await authStore.login(username.value, password.value);
|
const token = await authService.login(username.value, password.value);
|
||||||
|
authStore.setToken(token);
|
||||||
router.push('/');
|
router.push('/');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errorMessage.value = error.message || '登录失败';
|
errorMessage.value = error.message || '登录失败';
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { authService } from '@/api/authService';
|
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
@@ -8,13 +7,12 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
|
|
||||||
const isAuthenticated = computed(() => !!token.value);
|
const isAuthenticated = computed(() => !!token.value);
|
||||||
|
|
||||||
async function login(username, password) {
|
function setToken(newToken) {
|
||||||
const { data } = await authService.login(username, password);
|
token.value = newToken;
|
||||||
token.value = data.token;
|
localStorage.setItem('token', newToken);
|
||||||
user.value = data.user;
|
|
||||||
localStorage.setItem('token', data.token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
token.value = null;
|
token.value = null;
|
||||||
user.value = null;
|
user.value = null;
|
||||||
@@ -25,7 +23,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
token,
|
token,
|
||||||
user,
|
user,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
login,
|
logout,
|
||||||
logout
|
setToken
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user