From 2b7166fb47994476cb46f74bd451b9dd701114a3 Mon Sep 17 00:00:00 2001 From: lzh <52725224+Yourdaylight@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:11:44 +0800 Subject: [PATCH] feat:login®ister --- front/api/package-lock.json | 85 ++++++++++++++++++++ front/api/package.json | 3 +- front/api/src/functions/register.js | 82 ++++++++++++++++--- front/src/app/pages/login/login.component.ts | 5 +- front/src/app/services/api.service.ts | 2 +- front/src/environments/environment.prod.ts | 3 +- front/src/environments/environment.ts | 3 +- 7 files changed, 167 insertions(+), 16 deletions(-) diff --git a/front/api/package-lock.json b/front/api/package-lock.json index 0004b5e..e457e3e 100644 --- a/front/api/package-lock.json +++ b/front/api/package-lock.json @@ -18,11 +18,82 @@ "resolved": "https://registry.npmmirror.com/@fastify/busboy/-/busboy-2.1.0.tgz", "integrity": "sha512-+KpH+QxZU7O4675t3mnkQKcZZg56u+K/Ct2K+N2AZYNVK8kyeo/bI18tI8aPm3tvNNRyTWfj6s5tnGNlcbQRsA==" }, + "@mongodb-js/saslprep": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/@mongodb-js/saslprep/-/saslprep-1.1.1.tgz", + "integrity": "sha512-t7c5K033joZZMspnHg/gWPE4kandgc2OxE74aYOtGKfgB9VPuVJPix0H6fhmm2erj5PBJ21mqcx34lpIGtUCsQ==", + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmmirror.com/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==" + }, + "@types/whatwg-url": { + "version": "11.0.3", + "resolved": "https://registry.npmmirror.com/@types/whatwg-url/-/whatwg-url-11.0.3.tgz", + "integrity": "sha512-z1ELvMijRL1QmU7QuzDkeYXSF2+dXI0ITKoQsIoVKcNBOiK5RMmWy+pYYxJTHFt8vkpZe7UsvRErQwcxZkjoUw==", + "requires": { + "@types/webidl-conversions": "*" + } + }, + "bson": { + "version": "6.2.0", + "resolved": "https://registry.npmmirror.com/bson/-/bson-6.2.0.tgz", + "integrity": "sha512-ID1cI+7bazPDyL9wYy9GaQ8gEEohWvcUl/Yf0dIdutJxnmInEEyCsb4awy/OiBfall7zBA179Pahi3vCdFze3Q==" + }, "long": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/long/-/long-4.0.0.tgz", "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmmirror.com/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==" + }, + "mongodb": { + "version": "6.3.0", + "resolved": "https://registry.npmmirror.com/mongodb/-/mongodb-6.3.0.tgz", + "integrity": "sha512-tt0KuGjGtLUhLoU263+xvQmPHEGTw5LbcNC73EoFRYgSHwZt5tsoJC110hDyO1kjQzpgNrpdcSza9PknWN4LrA==", + "requires": { + "@mongodb-js/saslprep": "^1.1.0", + "bson": "^6.2.0", + "mongodb-connection-string-url": "^3.0.0" + } + }, + "mongodb-connection-string-url": { + "version": "3.0.0", + "resolved": "https://registry.npmmirror.com/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.0.tgz", + "integrity": "sha512-t1Vf+m1I5hC2M5RJx/7AtxgABy1cZmIPQRMXw+gEIPn/cZNF3Oiy+l0UIypUwVB5trcWHq3crg2g3uAR9aAwsQ==", + "requires": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" + } + }, + "punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmmirror.com/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==" + }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmmirror.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "requires": { + "memory-pager": "^1.0.2" + } + }, + "tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmmirror.com/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "requires": { + "punycode": "^2.3.0" + } + }, "undici": { "version": "5.28.2", "resolved": "https://registry.npmmirror.com/undici/-/undici-5.28.2.tgz", @@ -30,6 +101,20 @@ "requires": { "@fastify/busboy": "^2.0.0" } + }, + "webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmmirror.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==" + }, + "whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmmirror.com/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", + "requires": { + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" + } } } } diff --git a/front/api/package.json b/front/api/package.json index 164f749..364d272 100644 --- a/front/api/package.json +++ b/front/api/package.json @@ -7,7 +7,8 @@ "test": "echo \"No tests yet...\"" }, "dependencies": { - "@azure/functions": "^4.0.0" + "@azure/functions": "^4.0.0", + "mongodb": "^6.3.0" }, "devDependencies": {}, "main": "src/functions/*.js" diff --git a/front/api/src/functions/register.js b/front/api/src/functions/register.js index 2b26de7..683d087 100644 --- a/front/api/src/functions/register.js +++ b/front/api/src/functions/register.js @@ -1,25 +1,85 @@ const { app } = require('@azure/functions'); +const { MongoClient } = require('mongodb'); + +var connectionString = "mongodb://games:oQ5bO7YMfpu99oZMpKs0fjjypybyIMwBHJh7TmK8FYj1J41StnByDp1vxZ0huSXxlYbNLiRtNdvZACDb9cByMQ%3D%3D@games.mongo.cosmos.azure.com:10255/?ssl=true&retrywrites=false&maxIdleTimeMS=120000&appName=@games@"; +const client = new MongoClient(connectionString); + app.http('register', { - methods: ['GET', 'POST'], + methods: ['POST'], authLevel: 'anonymous', handler: async (request, context) => { - context.log(`Http function processed request for url "${request.url}"`); + try { + await client.connect(); + const db = client.db("games"); + const { username, password } = request.body; + const existingUser = await db.collection("user").findOne({ username }); - const name = request.query.get('name') || await request.text() || 'world'; - - return { body: `Hello, ${name}!` }; + if (!existingUser) { + await db.collection("user").insertOne({ username, password }); + context.res = { + status: 200, // HTTP 状态码 + body: JSON.stringify({ code: 200, msg: "SUCCESS", data: { /* 返回的数据 */ } }), + headers: { 'Content-Type': 'application/json' } + }; + } else { + context.res = { + status: 400, // 或其他合适的状态码 + body: JSON.stringify({ code: 400, msg: "Login or Registration failed" }) + }; + } + } catch (e) { + context.res = { + status: 500, + body: JSON.stringify({ code: 500, msg: e.message }), + headers: { 'Content-Type': 'application/json' } + }; + } finally { + if (client) { + client.close(); + } + return context.res; + } } }); + app.http('login', { - methods: ['GET', 'POST'], + methods: ['POST'], authLevel: 'anonymous', handler: async (request, context) => { - context.log(`Http function processed request for url "${request.url}"`); + try { + const { username, password } = request.body; + await client.connect(); + const db = client.db("games"); + const user = await db.collection("user").findOne({ username, password }); - const name = request.query.get('name') || await request.text() || 'world'; - - return { body: `Hhhh, ${name}!` }; + if (user) { + const token = jwt.sign({ username }, process.env["JWT_SECRET"], { expiresIn: '1h' }); + context.res = { + status: 200, // HTTP 状态码 + body: JSON.stringify({ code: 200, msg: "SUCCESS", data: { token } }), + headers: { 'Content-Type': 'application/json' } + }; + + } else { + context.res = { + status: 400, // 或其他合适的状态码 + body: JSON.stringify({ code: 400, msg: "Login or Registration failed" }) + }; + } + } catch (e) { + context.res = { + status: 500, + body: JSON.stringify({ code: 500, msg: e.message }), + headers: { 'Content-Type': 'application/json' } + } + } finally { + if (client) { + client.close(); + } + return context.res; + } } -}); \ No newline at end of file +}); + diff --git a/front/src/app/pages/login/login.component.ts b/front/src/app/pages/login/login.component.ts index ad98f96..864f7dd 100644 --- a/front/src/app/pages/login/login.component.ts +++ b/front/src/app/pages/login/login.component.ts @@ -27,10 +27,13 @@ export class LoginComponent implements OnInit { if (this.validateForm.valid) { console.log('submit', this.validateForm.value); let loginModel = Object.assign(this.validateForm.value, {}); - let url = type == 'login' ? '/games/login' : '/games/register'; + let url = type == 'login' ? '/login' : '/register'; console.log(url); this.apiService.post(url, loginModel).subscribe((res: any) => { const { code, msg, data } = res; + console.log(res); // 应该显示 JavaScript 对象 + console.log(res.code); // 访问对象的属性 + console.log(res.msg); if (code === 200) { if (type == 'login') { this.$message.success('Login Success!'); diff --git a/front/src/app/services/api.service.ts b/front/src/app/services/api.service.ts index a0a56e9..0fb7b53 100644 --- a/front/src/app/services/api.service.ts +++ b/front/src/app/services/api.service.ts @@ -7,7 +7,7 @@ import { NzMessageService } from 'ng-zorro-antd/message'; providedIn: 'root', }) export class ApiService { - _baseUrl = '/api/'; + _baseUrl = '/api'; constructor( private httpClient: HttpClient, private dialogService: NzMessageService diff --git a/front/src/environments/environment.prod.ts b/front/src/environments/environment.prod.ts index 3612073..932df64 100644 --- a/front/src/environments/environment.prod.ts +++ b/front/src/environments/environment.prod.ts @@ -1,3 +1,4 @@ export const environment = { - production: true + production: true, + apiUrl: '/api' }; diff --git a/front/src/environments/environment.ts b/front/src/environments/environment.ts index f56ff47..04c7b46 100644 --- a/front/src/environments/environment.ts +++ b/front/src/environments/environment.ts @@ -3,7 +3,8 @@ // The list of file replacements can be found in `angular.json`. export const environment = { - production: false + production: false, + apiUrl:'http://localhost:7071/api' }; /*