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

Allow remembering password #26

Merged
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
4 changes: 2 additions & 2 deletions js/otpmanager-main.js
100755 → 100644

Large diffs are not rendered by default.

Empty file modified js/otpmanager-main.js.LICENSE.txt
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion js/otpmanager-main.js.map
100755 → 100644

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion src/js/navbar/LargeDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
IconList,
IconMoonStars,
IconSettings,
IconSun
IconSun,
IconLockOff,
} from "@tabler/icons-react";
import { UserSettingContext } from "./../utils/UserSettingProvider";
import { navbarStyles } from "./Styles";
Expand All @@ -35,6 +36,9 @@ export function NavbarLargeDevice({
const { classes, cx } = navbarStyles();
const ChevronIcon = !showSettings ? IconChevronRight : IconChevronLeft;
const [userSetting, setUserSetting] = useContext(UserSettingContext);
const [passwordSaved, setPasswordSaved] = useState(
Boolean(localStorage.getItem("otpmanager_cached_password"))
);

return (
<>
Expand Down Expand Up @@ -134,6 +138,33 @@ export function NavbarLargeDevice({
(userSetting.darkMode ? "light mode" : "dark mode")}
</Text>
</Flex>

<Flex className={classes.innerLink} align="center">
<ActionIcon
variant="outline"
color="red"
onClick={(e) => {
localStorage.removeItem("otpmanager_cached_password");
setPasswordSaved(false);
}}
disabled={!passwordSaved}
sx={{
width: "20px",
height: "20px",
minWidth: "20px",
minHeight: "20px",
}}
title="Remove saved password"
>
<IconLockOff style={{ width: 16 }} />
</ActionIcon>
<Text
sx={{ fontSize: "14px", color: "#C1C2C5", marginLeft: "12px" }}
>
Remove saved password
</Text>
</Flex>

<div
href="#"
className={classes.link}
Expand Down
37 changes: 36 additions & 1 deletion src/js/navbar/SmallDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
IconList,
IconMoonStars,
IconSettings,
IconSun
IconSun,
IconLockOff,
} from "@tabler/icons-react";
import { UserSettingContext } from "./../utils/UserSettingProvider";
import { navbarStyles } from "./Styles";
Expand All @@ -38,6 +39,9 @@ export function NavbarSmallDevice({
const { classes, cx } = navbarStyles();
const ChevronIcon = !showSettings ? IconChevronRight : IconChevronLeft;
const [userSetting, setUserSetting] = useContext(UserSettingContext);
const [passwordSaved, setPasswordSaved] = useState(
Boolean(localStorage.getItem("otpmanager_cached_password"))
);

return (
<>
Expand Down Expand Up @@ -158,6 +162,37 @@ export function NavbarSmallDevice({
(userSetting.darkMode ? "light mode" : "dark mode")}
</Text>
</Flex>

<Flex className={classes.innerLink} align="center">
<ActionIcon
variant="outline"
color="red"
onClick={(e) => {
localStorage.removeItem("otpmanager_cached_password");
setPasswordSaved(false);
}}
disabled={!passwordSaved}
sx={{
width: "20px",
height: "20px",
minWidth: "20px",
minHeight: "20px",
}}
title="Remove saved password"
>
<IconLockOff style={{ width: 16 }} />
</ActionIcon>
<Text
sx={{
fontSize: "14px",
color: "#C1C2C5",
marginLeft: "12px",
}}
>
Remove saved password
</Text>
</Flex>

<div
href="#"
className={classes.link}
Expand Down
20 changes: 14 additions & 6 deletions src/js/utils/Password.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { useContext, useEffect } from "react";

import {
AppShell,
Expand All @@ -9,12 +9,9 @@ import {
Flex,
Loader,
Stack,
Text
Text,
} from "@mantine/core";
import {
showNotification,
updateNotification
} from "@mantine/notifications";
import { showNotification, updateNotification } from "@mantine/notifications";
import axios from "@nextcloud/axios";
import { generateUrl } from "@nextcloud/router";
import { IconCheck, IconX } from "@tabler/icons-react";
Expand Down Expand Up @@ -64,6 +61,9 @@ export const Password = ({ exists, setAuth }) => {
password: CryptoES.SHA256(values.password).toString(),
})
);
if (values.savePassword) {
localStorage.setItem("otpmanager_cached_password", values.password);
}
})
.catch((error) => {
if (error.response) {
Expand Down Expand Up @@ -99,6 +99,14 @@ export const Password = ({ exists, setAuth }) => {
});
}

useEffect(() => {
if (exists && localStorage.getItem("otpmanager_cached_password")) {
updatePassword({
password: localStorage.getItem("otpmanager_cached_password"),
});
}
}, [exists]);

return (
<AppShell padding="0" fixed={false} layout="alt">
<Flex
Expand Down
11 changes: 10 additions & 1 deletion src/js/utils/PasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
PasswordInput,
Popover,
Progress,
Text
Text,
Checkbox,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { IconCheck, IconX } from "@tabler/icons-react";
Expand Down Expand Up @@ -59,6 +60,7 @@ export default function PasswordForm({ exists, onSubmit, isChanging }) {
oldPassword: "",
password: "",
confirmPassword: "",
savePassword: false,
},

validate: {
Expand Down Expand Up @@ -140,6 +142,13 @@ export default function PasswordForm({ exists, onSubmit, isChanging }) {
{...form.getInputProps("confirmPassword")}
/>
)}
{!isChanging && exists && (
<Checkbox
label="Remember password"
mt="md"
{...form.getInputProps("savePassword")}
/>
)}
</Flex>
</form>
</Box>
Expand Down