|
| 1 | +import url from "url"; |
| 2 | +import { InternalServerError } from "../utils/customError"; |
| 3 | +import responseHandler from "../utils/responseHandler"; |
| 4 | +import { |
| 5 | + FacebookService, |
| 6 | + GitHubService, |
| 7 | + GoogleService, |
| 8 | + TwitterService, |
| 9 | +} from "../services"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Controller responsible for returning an authentication URL for the client |
| 13 | + * to initiate the Facebook's authentication flow. |
| 14 | + */ |
| 15 | +export const facebookController = (req, res, next) => { |
| 16 | + try { |
| 17 | + const facebookService = new FacebookService(); |
| 18 | + facebookService.authenticate(req.provider, req.socialCallback); |
| 19 | + const authenticationUrl = facebookService.getAuthenticationUrl(); |
| 20 | + |
| 21 | + responseHandler(res, 200, { authenticationUrl }); |
| 22 | + } catch (error) { |
| 23 | + next(new InternalServerError(error)); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Controller responsible for returning an authentication URL for the client |
| 29 | + * to initiate the GitHub's authentication flow. |
| 30 | + */ |
| 31 | +export const githubController = (req, res, next) => { |
| 32 | + try { |
| 33 | + const githubService = new GitHubService(); |
| 34 | + githubService.authenticate(req.provider, req.socialCallback); |
| 35 | + const authenticationUrl = githubService.getAuthenticationUrl(); |
| 36 | + |
| 37 | + responseHandler(res, 200, { authenticationUrl }); |
| 38 | + } catch (error) { |
| 39 | + next(new InternalServerError(error)); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Controller responsible for returning an authentication URL for the client |
| 45 | + * to initiate the Google's authentication flow. |
| 46 | + */ |
| 47 | +export const googleController = (req, res, next) => { |
| 48 | + try { |
| 49 | + const googleService = new GoogleService(); |
| 50 | + googleService.authenticate(req.provider, req.socialCallback); |
| 51 | + const authenticationUrl = googleService.getAuthenticationUrl(); |
| 52 | + |
| 53 | + responseHandler(res, 200, { authenticationUrl }); |
| 54 | + } catch (error) { |
| 55 | + next(new InternalServerError(error)); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * Controller responsible for returning an authentication URL for the client |
| 61 | + * to initiate the Twitter's authentication flow. |
| 62 | + */ |
| 63 | +export const twitterController = async (req, res, next) => { |
| 64 | + try { |
| 65 | + const twitterService = new TwitterService(); |
| 66 | + twitterService.authenticate(req.provider, req.socialCallback); |
| 67 | + const authenticationUrl = await twitterService.getAuthenticationUrl(); |
| 68 | + |
| 69 | + responseHandler(res, 200, { authenticationUrl }); |
| 70 | + } catch (error) { |
| 71 | + next(new InternalServerError(error)); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Controller responsible for handling the callback from Facebook's authentication flow and |
| 77 | + * returning the user data to the client via a redirect to the client's provided |
| 78 | + * socialCallback. |
| 79 | + */ |
| 80 | +export const facebookCallbackController = async (req, res, next) => { |
| 81 | + try { |
| 82 | + // The state contains the client ID. |
| 83 | + const { state, code } = req.query; |
| 84 | + const facebookService = new FacebookService(); |
| 85 | + const { profile, socialCallback } = await facebookService.getData( |
| 86 | + state.toString(), |
| 87 | + code |
| 88 | + ); |
| 89 | + |
| 90 | + let redirectUrl; |
| 91 | + |
| 92 | + if (profile && profile.facebookId) { |
| 93 | + redirectUrl = url.format({ |
| 94 | + pathname: socialCallback, |
| 95 | + query: { |
| 96 | + success: true, |
| 97 | + id: profile.facebookId, |
| 98 | + isVerified: profile.isVerified, |
| 99 | + firstname: profile.firstname, |
| 100 | + lastname: profile.lastname, |
| 101 | + username: profile.username, |
| 102 | + email: profile.email, |
| 103 | + photo: profile.photo, |
| 104 | + }, |
| 105 | + }); |
| 106 | + } else { |
| 107 | + redirectUrl = url.format({ |
| 108 | + pathname: socialCallback, |
| 109 | + query: { success: false }, |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + res.redirect(redirectUrl); |
| 114 | + } catch (error) { |
| 115 | + next(new InternalServerError(error)); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Controller responsible for handling the callback from GitHub's authentication flow and |
| 121 | + * returning the user data to the client via a redirect to the client's provided |
| 122 | + * socialCallback. |
| 123 | + */ |
| 124 | +export const githubCallbackController = async (req, res, next) => { |
| 125 | + try { |
| 126 | + // The state contains the client ID. |
| 127 | + const { state, code } = req.query; |
| 128 | + const githubService = new GitHubService(); |
| 129 | + const { profile, socialCallback } = await githubService.getData( |
| 130 | + state.toString(), |
| 131 | + code |
| 132 | + ); |
| 133 | + |
| 134 | + let redirectUrl; |
| 135 | + |
| 136 | + if (profile && profile.githubId) { |
| 137 | + redirectUrl = url.format({ |
| 138 | + pathname: socialCallback, |
| 139 | + query: { |
| 140 | + success: true, |
| 141 | + id: profile.githubId, |
| 142 | + isVerified: profile.isVerified, |
| 143 | + firstname: profile.firstname, |
| 144 | + lastname: profile.lastname, |
| 145 | + username: profile.username, |
| 146 | + email: profile.email, |
| 147 | + photo: profile.photo, |
| 148 | + }, |
| 149 | + }); |
| 150 | + } else { |
| 151 | + redirectUrl = url.format({ |
| 152 | + pathname: socialCallback, |
| 153 | + query: { success: false }, |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + res.redirect(redirectUrl); |
| 158 | + } catch (error) { |
| 159 | + next(new InternalServerError(error)); |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +/** |
| 164 | + * Controller responsible for handling the callback from Google's authentication flow and |
| 165 | + * returning the user data to the client via a redirect to the client's provided |
| 166 | + * socialCallback. |
| 167 | + */ |
| 168 | +export const googleCallbackController = async (req, res, next) => { |
| 169 | + try { |
| 170 | + // The state contains the client ID. |
| 171 | + const { state, code } = req.query; |
| 172 | + |
| 173 | + const googleService = new GoogleService(); |
| 174 | + const { profile, socialCallback } = await googleService.getData( |
| 175 | + state.toString(), |
| 176 | + code |
| 177 | + ); |
| 178 | + |
| 179 | + let redirectUrl; |
| 180 | + |
| 181 | + if (profile && profile.googleId) { |
| 182 | + redirectUrl = url.format({ |
| 183 | + pathname: socialCallback, |
| 184 | + query: { |
| 185 | + success: true, |
| 186 | + id: profile.googleId, |
| 187 | + isVerified: profile.isVerified, |
| 188 | + firstname: profile.firstname, |
| 189 | + lastname: profile.lastname, |
| 190 | + username: profile.username, |
| 191 | + email: profile.email, |
| 192 | + photo: profile.photo, |
| 193 | + }, |
| 194 | + }); |
| 195 | + } else { |
| 196 | + redirectUrl = url.format({ |
| 197 | + pathname: socialCallback, |
| 198 | + query: { success: false }, |
| 199 | + }); |
| 200 | + } |
| 201 | + |
| 202 | + res.redirect(redirectUrl); |
| 203 | + } catch (error) { |
| 204 | + next(new InternalServerError(error)); |
| 205 | + } |
| 206 | +}; |
| 207 | + |
| 208 | +/** |
| 209 | + * Controller responsible for handling the callback from Twitter's authentication flow and |
| 210 | + * returning the user data to the client via a redirect to the client's provided |
| 211 | + * socialCallback. |
| 212 | + */ |
| 213 | +export const twitterCallbackController = async (req, res, next) => { |
| 214 | + try { |
| 215 | + // The state contains the client ID. |
| 216 | + const { oauth_token, oauth_verifier } = req.query; |
| 217 | + |
| 218 | + const twitterService = new TwitterService(); |
| 219 | + const { profile, socialCallback } = await twitterService.getData( |
| 220 | + oauth_token.toString(), |
| 221 | + oauth_verifier.toString() |
| 222 | + ); |
| 223 | + |
| 224 | + let redirectUrl; |
| 225 | + |
| 226 | + if (profile && profile.twitterId) { |
| 227 | + redirectUrl = url.format({ |
| 228 | + pathname: socialCallback, |
| 229 | + query: { |
| 230 | + success: true, |
| 231 | + id: profile.twitterId, |
| 232 | + isVerified: profile.isVerified, |
| 233 | + firstname: profile.firstname, |
| 234 | + lastname: profile.lastname, |
| 235 | + username: profile.username, |
| 236 | + email: profile.email, |
| 237 | + photo: profile.photo, |
| 238 | + }, |
| 239 | + }); |
| 240 | + } else { |
| 241 | + redirectUrl = url.format({ |
| 242 | + pathname: socialCallback, |
| 243 | + query: { success: false }, |
| 244 | + }); |
| 245 | + } |
| 246 | + |
| 247 | + res.redirect(redirectUrl); |
| 248 | + } catch (error) { |
| 249 | + next(new InternalServerError(error)); |
| 250 | + } |
| 251 | +}; |
0 commit comments