propro-utils 1.4.79 → 1.4.81

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.79",
3
+ "version": "1.4.81",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -48,6 +48,8 @@ class AuthMiddleware {
48
48
 
49
49
  initializeRoutes() {
50
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);
@@ -68,8 +70,75 @@ 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?returnTokens=${returnTokens}`,
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, tokens } = response.data;
93
+ const user = await checkIfUserExists(this.userSchema, account.accountId);
94
+
95
+ if (returnTokens === 'true') {
96
+ res.status(response.status).json({ account, user, tokens });
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, password } = req.body;
109
+ const { returnTokens } = req.query;
110
+
111
+ try {
112
+ const response = await this.proxyToAuthServer(
113
+ req,
114
+ `/api/v1/auth/register?returnTokens=${returnTokens}`,
115
+ 'POST',
116
+ {
117
+ name,
118
+ email,
119
+ password,
120
+ clientId: this.options.clientId,
121
+ redirectUri: this.options.redirectUri,
122
+ returnTokens,
123
+ }
124
+ );
125
+
126
+ const { account, tokens } = response.data;
127
+ const user = await checkIfUserExists(this.userSchema, account.accountId);
128
+
129
+ if (returnTokens === 'true') {
130
+ res.status(response.status).json({ account, user, tokens });
131
+ } else {
132
+ const { urlToRedirect } = response.data;
133
+ res.status(response.status).json({ urlToRedirect });
134
+ }
135
+ } catch (error) {
136
+ this.handleProxyError(error, res);
137
+ }
138
+ };
139
+
71
140
  handleCallback = async (req, res) => {
72
- const { code } = req.query;
141
+ const { code, returnTokens } = req.query;
73
142
  if (!code) {
74
143
  return res.status(400).send('No code received');
75
144
  }
@@ -84,6 +153,10 @@ class AuthMiddleware {
84
153
  );
85
154
 
86
155
  const user = await checkIfUserExists(this.userSchema, account.accountId);
156
+
157
+ if (returnTokens === 'true') {
158
+ return res.status(200).json({ account, user, tokens });
159
+ }
87
160
  setAuthCookies(res, tokens, account, user, redirectUrl);
88
161
 
89
162
  res.redirect(formatRedirectUrl(redirectUrl));