@@ -7,6 +7,7 @@ import { Request, Response } from 'express';
7
7
import { I18nService } from 'nestjs-i18n' ;
8
8
// #endregion
9
9
// #region Imports Local
10
+ import { Login , LoginEmail } from '@lib/types/auth' ;
10
11
import { User } from '@lib/types/user.dto' ;
11
12
import { LogService } from '@app/logger' ;
12
13
import { ConfigService } from '@app/config' ;
@@ -45,7 +46,7 @@ export class AuthResolver {
45
46
* @method login
46
47
* @param {string } username Username
47
48
* @param {string } password Password
48
- * @returns {boolean } True if a login successfull
49
+ * @returns {Login } The login response
49
50
* @throws {GraphQLError }
50
51
*/
51
52
@Query ( )
@@ -54,7 +55,9 @@ export class AuthResolver {
54
55
@Args ( 'password' ) password : string ,
55
56
@Context ( 'req' ) req : Request ,
56
57
@Context ( 'res' ) res : Response ,
57
- ) : Promise < boolean > {
58
+ ) : Promise < Login > {
59
+ let email : LoginEmail = { login : false } ;
60
+
58
61
const user = await this . authService
59
62
. login ( { username : username . toLowerCase ( ) , password } )
60
63
. catch ( async ( error : Error ) => {
@@ -70,44 +73,19 @@ export class AuthResolver {
70
73
} ) ;
71
74
72
75
if ( user . profile . email ) {
73
- await this . authService
74
- . loginEmail ( user . profile . email , password )
75
- . then (
76
- ( response ) => {
77
- const { sessid, sessauth } = response . data ;
78
- if ( sessid && sessauth ) {
79
- const options = {
80
- // domain: '.portal.i-npz.ru',
81
- maxAge : this . configService . get < number > ( 'SESSION_COOKIE_TTL' ) ,
82
- } ;
83
-
84
- res . cookie ( 'roundcube_sessid' , sessid , options ) ;
85
- res . cookie ( 'roundcube_sessauth' , sessauth , options ) ;
86
-
87
- req ! . session ! . mailSession = {
88
- sessid,
89
- sessauth,
90
- } ;
91
-
92
- return true ;
93
- }
94
-
95
- throw new Error ( 'Undefined mailSession error.' ) ;
96
- } ,
97
- ( ) => {
98
- return false ;
99
- } ,
100
- )
101
- . catch ( ( error : Error ) => {
102
- this . logService . error ( 'Unable to login in mail' , error , AuthResolver . name ) ;
103
-
104
- return false ;
105
- } ) ;
76
+ email = await this . authService . loginEmail ( user . profile . email , password , req , res ) . catch ( ( error : Error ) => {
77
+ this . logService . error ( 'Unable to login in mail' , error , AuthResolver . name ) ;
78
+
79
+ return {
80
+ login : false ,
81
+ error : error . toString ( ) ,
82
+ } ;
83
+ } ) ;
106
84
}
107
85
108
86
req ! . session ! . password = password ;
109
87
110
- return true ;
88
+ return { login : true , email } ;
111
89
}
112
90
113
91
/**
@@ -120,48 +98,19 @@ export class AuthResolver {
120
98
*/
121
99
@Query ( )
122
100
async loginEmail (
123
- @CurrentUser ( ) user : User ,
124
- @PasswordFrontend ( ) password : string ,
125
101
@Context ( 'req' ) req : Request ,
126
102
@Context ( 'res' ) res : Response ,
127
- ) : Promise < boolean > {
128
- if ( user . profile . email ) {
129
- return this . authService
130
- . loginEmail ( user . profile . email , password )
131
- . then (
132
- ( response ) => {
133
- const { sessid, sessauth } = response . data ;
134
- if ( sessid && sessauth ) {
135
- const options = {
136
- // domain: '.portal.i-npz.ru',
137
- maxAge : this . configService . get < number > ( 'SESSION_COOKIE_TTL' ) ,
138
- } ;
139
-
140
- res . cookie ( 'roundcube_sessid' , sessid , options ) ;
141
- res . cookie ( 'roundcube_sessauth' , sessauth , options ) ;
142
-
143
- req ! . session ! . mailSession = {
144
- sessid,
145
- sessauth,
146
- } ;
147
-
148
- return true ;
149
- }
150
-
151
- throw new Error ( 'Undefined mailSession error.' ) ;
152
- } ,
153
- ( ) => {
154
- return false ;
155
- } ,
156
- )
157
- . catch ( ( error : Error ) => {
158
- this . logService . error ( 'Unable to login in mail' , error , AuthResolver . name ) ;
159
-
160
- return false ;
161
- } ) ;
162
- }
163
-
164
- return false ;
103
+ @CurrentUser ( ) user ?: User ,
104
+ @PasswordFrontend ( ) password ?: string ,
105
+ ) : Promise < LoginEmail > {
106
+ return this . authService . loginEmail ( user ?. profile . email || '' , password || '' , req , res ) . catch ( ( error : Error ) => {
107
+ this . logService . error ( 'Unable to login in mail' , error , AuthResolver . name ) ;
108
+
109
+ return {
110
+ login : false ,
111
+ error : error . toString ( ) ,
112
+ } ;
113
+ } ) ;
165
114
}
166
115
167
116
/**
0 commit comments