propro-utils 1.4.78 → 1.4.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.4.78",
3
+ "version": "1.4.80",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -47,7 +47,9 @@ class AuthMiddleware {
47
47
  }
48
48
 
49
49
  initializeRoutes() {
50
- this.router.get('/api/auth', this.handleAuth);
50
+ this.router.post('/api/auth', this.handleAuth);
51
+ this.router.get('/api/login', this.handleLogin);
52
+ this.router.get('/api/register', this.handleRegister);
51
53
  this.router.get('/api/callback', this.handleCallback);
52
54
  this.router.post('/api/refreshToken', this.handleRefreshToken);
53
55
  this.router.post('/api/logout', this.handleLogout);
@@ -57,7 +59,7 @@ class AuthMiddleware {
57
59
  this.router.patch('api/profile/2fa', this.handleTwoFactorAuth);
58
60
  this.router.patch('/api/profile/avatar', this.handleAvatarUpdate);
59
61
  this.router.patch(
60
- 'api/app/settings',
62
+ '/api/app/settings',
61
63
  authValidation(this.redisClient, this.userSchema, ['user']),
62
64
  this.handleAppSettings
63
65
  );
@@ -68,6 +70,74 @@ class AuthMiddleware {
68
70
  res.status(200).json({ redirectUrl });
69
71
  };
70
72
 
73
+ handleLogin = async (req, res) => {
74
+ const { email, username, password } = req.body;
75
+ const { returnTokens } = req.query;
76
+
77
+ try {
78
+ const response = await this.proxyToAuthServer(
79
+ req,
80
+ '/api/v1/auth/login',
81
+ 'POST',
82
+ {
83
+ email,
84
+ username,
85
+ password,
86
+ clientId: this.options.clientId,
87
+ redirectUri: this.options.redirectUri,
88
+ returnTokens,
89
+ }
90
+ );
91
+
92
+ const { account } = response.data;
93
+ const user = await checkIfUserExists(this.userSchema, account.accountId);
94
+
95
+ if (returnTokens === 'true') {
96
+ res.status(response.status).json(response.data, user);
97
+ } else {
98
+ const { tokens, urlToRedirect } = response.data;
99
+ setAuthCookies(res, tokens, account, user, this.options.appUrl);
100
+ res.status(response.status).json({ urlToRedirect });
101
+ }
102
+ } catch (error) {
103
+ this.handleProxyError(error, res);
104
+ }
105
+ };
106
+
107
+ handleRegister = async (req, res) => {
108
+ const { name, email, username, password } = req.body;
109
+ const { returnTokens } = req.query;
110
+
111
+ try {
112
+ const response = await this.proxyToAuthServer(
113
+ req,
114
+ '/api/v1/auth/register',
115
+ 'POST',
116
+ {
117
+ name,
118
+ email,
119
+ username,
120
+ password,
121
+ clientId: this.options.clientId,
122
+ redirectUri: this.options.redirectUri,
123
+ returnTokens,
124
+ }
125
+ );
126
+
127
+ const { account } = response.data;
128
+ const user = await checkIfUserExists(this.userSchema, account.accountId);
129
+
130
+ if (returnTokens === 'true') {
131
+ res.status(response.status).json(response.data, user);
132
+ } else {
133
+ const { urlToRedirect } = response.data;
134
+ res.status(response.status).json({ urlToRedirect });
135
+ }
136
+ } catch (error) {
137
+ this.handleProxyError(error, res);
138
+ }
139
+ };
140
+
71
141
  handleCallback = async (req, res) => {
72
142
  const { code } = req.query;
73
143
  if (!code) {