Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
egg-websocket
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huanglu
egg-websocket
Commits
1c3f9c81
Commit
1c3f9c81
authored
May 24, 2021
by
huanglu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: bugs
parent
e28458d4
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
59 additions
and
40 deletions
+59
-40
auth.ts
app/controller/auth.ts
+8
-2
auth.ts
app/middleware/auth.ts
+2
-4
error_handler.ts
app/middleware/error_handler.ts
+1
-2
auth.ts
app/router/auth.ts
+2
-1
index.ts
app/service/auth/index.ts
+0
-29
user.ts
app/service/auth/user.ts
+44
-0
index.d.ts
typings/app/service/index.d.ts
+2
-2
No files found.
app/controller/auth.ts
View file @
1c3f9c81
...
@@ -4,11 +4,17 @@ export default class Auth extends Controller {
...
@@ -4,11 +4,17 @@ export default class Auth extends Controller {
async
login
()
{
async
login
()
{
const
{
ctx
}
=
this
const
{
ctx
}
=
this
console
.
log
(
'====: '
,
ctx
.
request
.
body
,
ctx
.
data
)
const
data
=
await
ctx
.
service
.
auth
.
user
.
login
()
const
data
=
await
ctx
.
service
.
auth
.
index
.
register
()
ctx
.
body
=
ctx
.
helper
.
success
(
data
)
ctx
.
body
=
ctx
.
helper
.
success
(
data
)
}
}
async
register
()
{
console
.
log
(
'====== register'
)
const
{
ctx
}
=
this
const
data
=
await
ctx
.
service
.
auth
.
user
.
register
()
ctx
.
body
=
ctx
.
helper
.
success
(
data
,
'注册成功'
)
}
async
getUserInfo
()
{
async
getUserInfo
()
{
this
.
ctx
.
body
=
{
this
.
ctx
.
body
=
{
ll
:
'sfsfs'
ll
:
'sfsfs'
...
...
app/middleware/auth.ts
View file @
1c3f9c81
...
@@ -5,10 +5,9 @@ export default function jwt(options: EggAppConfig['jwt']) {
...
@@ -5,10 +5,9 @@ export default function jwt(options: EggAppConfig['jwt']) {
//拿到不需要验证的token的路由
//拿到不需要验证的token的路由
const
routerAuth
=
ctx
.
app
.
config
.
routerNotAuth
;
const
routerAuth
=
ctx
.
app
.
config
.
routerNotAuth
;
//获取当前路由
//获取当前路由
const
url
=
ctx
.
url
;
const
path
=
ctx
.
path
;
console
.
log
(
'=====: '
,
routerAuth
,
url
)
//判断当前路由是否需要验证token
//判断当前路由是否需要验证token
const
flag
=
routerAuth
.
includes
(
url
)
const
flag
=
routerAuth
.
includes
(
path
)
if
(
flag
)
{
if
(
flag
)
{
await
next
();
await
next
();
}
else
{
}
else
{
...
@@ -26,7 +25,6 @@ export default function jwt(options: EggAppConfig['jwt']) {
...
@@ -26,7 +25,6 @@ export default function jwt(options: EggAppConfig['jwt']) {
data
:
null
data
:
null
}
}
}
}
}
}
};
};
}
}
app/middleware/error_handler.ts
View file @
1c3f9c81
...
@@ -5,12 +5,11 @@ module.exports = () => {
...
@@ -5,12 +5,11 @@ module.exports = () => {
try
{
try
{
await
next
();
await
next
();
}
catch
(
err
)
{
}
catch
(
err
)
{
ctx
.
app
.
emit
(
"error"
,
err
,
ctx
);
const
status
=
err
.
status
||
500
;
const
status
=
err
.
status
||
500
;
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
const
error
=
status
===
500
&&
err
.
message
||
"Internal Server Error"
;
const
error
=
status
===
500
&&
err
.
message
||
"Internal Server Error"
;
// 从 error 对象上读出各个属性,设置到响应中
// 从 error 对象上读出各个属性,设置到响应中
ctx
.
status
=
status
ctx
.
body
=
ctx
.
helper
.
fail
(
error
)
ctx
.
body
=
ctx
.
helper
.
fail
(
error
)
}
}
};
};
...
...
app/router/auth.ts
View file @
1c3f9c81
...
@@ -3,5 +3,5 @@ import { Application } from "egg";
...
@@ -3,5 +3,5 @@ import { Application } from "egg";
export
default
(
app
:
Application
)
=>
{
export
default
(
app
:
Application
)
=>
{
const
{
controller
}
=
app
const
{
controller
}
=
app
app
.
router
.
post
(
'/login'
,
controller
.
auth
.
login
)
app
.
router
.
post
(
'/login'
,
controller
.
auth
.
login
)
app
.
router
.
get
(
'/userInfo'
,
controller
.
auth
.
getUserInfo
)
app
.
router
.
post
(
'/register'
,
controller
.
auth
.
register
)
};
};
\ No newline at end of file
app/service/auth/index.ts
deleted
100644 → 0
View file @
e28458d4
import
{
Service
}
from
"egg"
;
export
default
class
AuthService
extends
Service
{
async
register
()
{
const
{
ctx
,
app
}
=
this
const
{
user
,
password
}
=
ctx
.
request
.
body
||
{}
if
(
!
user
||
!
password
)
{
throw
new
Error
(
'user or password not null '
)
}
const
token
=
this
.
app
.
jwt
.
sign
({
user
,
password
},
app
.
config
.
jwt
.
secret
)
if
(
token
)
{
// const value = await (app as any).mysql.update('geek-user', { username: user, password, token }, { id: 5 })
const
sql
=
`select ? from ?`
console
.
log
(
'get user : '
,
sql
)
const
value
=
await
(
app
as
any
).
mysql
.
query
(
sql
,
[
'username'
,
'geek-user'
])
console
.
log
(
'get user : '
,
value
)
return
{
token
,
user
}
}
else
{
throw
new
Error
(
'register failed'
)
}
}
}
\ No newline at end of file
app/service/auth/user.ts
0 → 100644
View file @
1c3f9c81
import
{
Service
}
from
"egg"
;
export
default
class
AuthService
extends
Service
{
async
register
()
{
const
{
ctx
,
app
}
=
this
const
{
username
,
password
}
=
ctx
.
request
.
body
||
{}
if
(
!
username
||
!
password
)
{
throw
new
Error
(
'user or password not null '
)
}
try
{
await
(
app
as
any
).
mysql
.
insert
(
'user'
,
{
username
,
password
})
return
{
username
}
}
catch
(
error
)
{
throw
error
}
}
async
login
()
{
const
{
ctx
,
app
}
=
this
const
{
username
,
password
}
=
ctx
.
request
.
body
||
{}
if
(
!
username
||
!
password
)
{
throw
new
Error
(
'user or password not null '
)
}
try
{
const
result
=
await
(
app
as
any
).
mysql
.
get
(
'user'
,
{
username
})
// const token = this.app.jwt.sign({
// username,
// password
// }, app.config.jwt.secret)
// await (app as any).mysql.update('user', { password, token }, { username })
// return {
// token,
// username
// }
console
.
log
(
'query user: '
,
result
)
return
result
}
catch
(
error
)
{
throw
error
}
}
}
\ No newline at end of file
typings/app/service/index.d.ts
View file @
1c3f9c81
...
@@ -6,12 +6,12 @@ type AnyClass = new (...args: any[]) => any;
...
@@ -6,12 +6,12 @@ type AnyClass = new (...args: any[]) => any;
type
AnyFunc
<
T
=
any
>
=
(...
args
:
any
[])
=>
T
;
type
AnyFunc
<
T
=
any
>
=
(...
args
:
any
[])
=>
T
;
type
CanExportFunc
=
AnyFunc
<
Promise
<
any
>>
|
AnyFunc
<
IterableIterator
<
any
>>
;
type
CanExportFunc
=
AnyFunc
<
Promise
<
any
>>
|
AnyFunc
<
IterableIterator
<
any
>>
;
type
AutoInstanceType
<
T
,
U
=
T
extends
CanExportFunc
?
T
:
T
extends
AnyFunc
?
ReturnType
<
T
>
:
T
>
=
U
extends
AnyClass
?
InstanceType
<
U
>
:
U
;
type
AutoInstanceType
<
T
,
U
=
T
extends
CanExportFunc
?
T
:
T
extends
AnyFunc
?
ReturnType
<
T
>
:
T
>
=
U
extends
AnyClass
?
InstanceType
<
U
>
:
U
;
import
ExportAuth
Index
from
'../../../app/service/auth/index'
;
import
ExportAuth
User
from
'.../../../app/service/auth/user
declare module '
egg
' {
declare module '
egg
' {
interface IService {
interface IService {
auth: {
auth: {
index
:
AutoInstanceType
<
typeof
ExportAuthIndex
>
;
user: AutoInstanceType<typeof ExportAuthUser
>;
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment