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

Small login bugs #1788

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ export default {
window.location.pathname == '/tools' ||
window.location.pathname == '/tools/'
) {
// TODO: There's some sort of race condition here which makes this flaky if the user went to '/login' with
// no `?redirect=` query param while they were already authenticated. That sends them to '/', causing
// another redirect here that gets stepped on, so they're left at '/'. The nav bar shows, so they can click
// on a tool, or refresh the page to make this redirect actually happen.
for (let key of Object.keys(this.shownTools)) {
if (this.appNav[key].shown) {
history.replaceState(null, '', this.appNav[key].url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<v-row>
<v-btn
type="submit"
@click.prevent="verifyPassword"
@click.prevent="() => verifyPassword()"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work since verifyPassword takes 2 parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All js function arguments default to undefined if you don't pass them in. So for when it's called from here, the ||= operator on line 163 will set token, and the ! operator on line 181 will negate undefined to set this.showAlert to true.

This line could be written as @click.prevent="() => verifyPassword(password, false)" (or even verifyPassword(null, false)) but it's unnecessary to be explicit

size="large"
color="success"
:disabled="!formValid"
Expand Down Expand Up @@ -138,6 +138,11 @@ export default {
}
})
},
mounted: function () {
if (localStorage.openc3Token) {
this.verifyPassword(localStorage.openc3Token, true)
}
},
methods: {
showReset: function () {
this.reset = true
Expand All @@ -147,28 +152,33 @@ export default {
const redirect = new URLSearchParams(window.location.search).get(
'redirect',
)
if (redirect.startsWith('/tools/')) {
if (redirect?.startsWith('/tools/')) {
// Valid relative redirect URL
window.location = decodeURI(redirect)
} else {
window.location = '/'
}
},
verifyPassword: function () {
verifyPassword: function (token, noAlert) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever called with noAlert set to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called with undefined as discussed above

token ||= this.password
this.showAlert = false
Api.post('/openc3-api/auth/verify', {
data: {
token: this.password,
token,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token key not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data: { token }, is equivalent to data: { token: token }, but reads cleaner imo

},
...this.options,
})
.then((response) => {
this.login(response)
})
.catch((error) => {
this.alert = 'Incorrect password'
if (error?.status === 401) {
this.alert = 'Incorrect password'
} else {
this.alert = error.message || 'Something went wrong...'
}
this.alertType = 'warning'
this.showAlert = true
this.showAlert = !noAlert
})
},
setPassword: function () {
Expand Down
Loading