Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AccountService and Repository #512

Open
fcaps opened this issue Dec 2, 2023 · 0 comments
Open

Add AccountService and Repository #512

fcaps opened this issue Dec 2, 2023 · 0 comments

Comments

@fcaps
Copy link
Collaborator

fcaps commented Dec 2, 2023

All api-calls, that are doing some kind of account manipulation like: changePW, linkSteam, changeEmail etc.
should be only accessed through a AccountService/Repo.
This would have multiple benefits.

  1. auto refresh token if expired (via JavaApiService)
  2. no old "request" style code
  3. error-handling can be done at a single place
  4. clear parameters and return values
  5. they are reusable (saw some duplicated code)
  6. less testing hassle with mocking http, the service could also be injected in the router/controller

A solution could look something like:
Old:

        request.post({
            url: process.env.API_URL + '/users/changePassword',
            headers: { Authorization: 'Bearer ' + req.requestContainer.get('UserService').getUser()?.oAuthPassport.token },
            form: { currentPassword: oldPassword, newPassword }
        }, function (err, res, body) {
            if (err || res.statusCode !== 200) {
                error.parseApiErrors(body, flash)
                return overallRes.render('account/changePassword', { flash })
            }

            // Successfully reset password
            flash.class = 'alert-success'
            flash.messages = [{ msg: 'Your password was changed successfully. Please use the new password to log in!' }]
            flash.type = 'Success!'

            overallRes.render('account/changePassword', { flash })
        })

new:

        accountService.changePassword(oldPassword, newPassword).then(() => {
            res.render('account/changePassword', { success: true })
        }).catch((apiError) => {
            res.render('account/changePassword', { error: apiError })
        })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant