Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: consolidate password check into a helper. add a reset password section into settings

Jonathan Summers-Muirdbc468a58add
diff --git a/studio/components/ui/PasswordStrengthBar.tsx b/studio/components/ui/PasswordStrengthBar.tsx
new file mode 100644
index 00000000000..d0b5240d9db
--- /dev/null
+++ b/studio/components/ui/PasswordStrengthBar.tsx
@@ -0,0 +1,52 @@
+import {
+ DEFAULT_MINIMUM_PASSWORD_STRENGTH,
+ PASSWORD_STRENGTH_COLOR,
+ PASSWORD_STRENGTH_PERCENTAGE,
+} from 'lib/constants'
+
+interface Props {
+ passwordStrengthScore: number
+ passwordStrengthMessage: string
+ password: string
+}
+
+const PasswordStrengthBar = ({
+ passwordStrengthScore = 0,
+ passwordStrengthMessage = '',
+ password = '',
+}: Props) => {
+ return (
+ <>
+ {password && (
+ <div
+ aria-valuemax={100}
+ aria-valuemin={0}
+ aria-valuenow={(PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore]}
+ aria-valuetext={(PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore]}
+ role="progressbar"
+ className="mb-2 bg-bg-alt-light dark:bg-bg-alt-dark rounded overflow-hidden transition-all border dark:border-dark"
+ >
+ <div
+ style={{
+ width: (PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore],
+ }}
+ className={`relative h-2 w-full ${
+ (PASSWORD_STRENGTH_COLOR as any)[passwordStrengthScore]
+ } transition-all duration-500 ease-out shadow-inner`}
+ ></div>
+ </div>
+ )}
+ <span
+ className={
+ passwordStrengthScore >= DEFAULT_MINIMUM_PASSWORD_STRENGTH ? 'text-green-600' : ''
+ }
+ >
+ {passwordStrengthMessage
+ ? passwordStrengthMessage
+ : 'This is the password to your postgres database, so it must be a strong password and hard to guess.'}
+ </span>
+ </>
+ )
+}
+
+export default PasswordStrengthBar
diff --git a/studio/lib/constants/infrastructure.ts b/studio/lib/constants/infrastructure.ts
index 0baa0f5063d..5c138eb240a 100644
--- a/studio/lib/constants/infrastructure.ts
+++ b/studio/lib/constants/infrastructure.ts
@@ -117,7 +117,7 @@ export const PASSWORD_STRENGTH = {
export const PASSWORD_STRENGTH_COLOR = {
0: 'bg-red-500',
- 1: 'bg-red-400',
+ 1: 'bg-red-500',
2: 'bg-yellow-500',
3: 'bg-yellow-500',
4: 'bg-green-500',
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 9570b1d1f67..286697320bf 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -1,3 +1,6 @@
+import { post } from 'lib/common/fetch'
+import { PASSWORD_STRENGTH, DEFAULT_MINIMUM_PASSWORD_STRENGTH, API_URL } from 'lib/constants'
+
export const tryParseJson = (jsonString: any) => {
try {
let parsed = JSON.parse(jsonString)
@@ -161,3 +164,37 @@ export const copyToClipboard = (str: string, callback = () => {}) => {
console.warn('Unable to copy to clipboard')
}
}
+
+export async function passwordStrength(value: string) {
+ let message = ''
+ let warning = ''
+ let strength = 0
+
+ if (value && value !== '') {
+ const response = await post(`${API_URL}/profile/password-check`, { password: value })
+ if (!response.error) {
+ const { result } = response
+ const score = (PASSWORD_STRENGTH as any)[result.score]
+ const suggestions = result.feedback?.suggestions ? result.feedback.suggestions.join(' ') : ''
+
+ // set message :string
+ message = `${score} ${suggestions}`
+
+ // set strength :number
+ strength = result.score
+
+ // warning message for anything below 4 strength :string
+ if (result.score < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
+ warning = `${
+ result?.feedback?.warning ? result?.feedback?.warning + '.' : ''
+ } You need a stronger password.`
+ }
+ }
+ }
+
+ return {
+ message,
+ warning,
+ strength,
+ }
+}
diff --git a/studio/pages/new/[slug].tsx b/studio/pages/new/[slug].tsx
index 6927b5634d0..dc27ce64e84 100644
--- a/studio/pages/new/[slug].tsx
+++ b/studio/pages/new/[slug].tsx
@@ -7,14 +7,7 @@ import { useRef, useState, useEffect } from 'react'
import { debounce, isUndefined, values } from 'lodash'
import { toJS } from 'mobx'
import { observer } from 'mobx-react-lite'
-import {
- Button,
- Typography,
- Listbox,
- IconUsers,
- IconAlertCircle,
- IconDollarSign,
-} from '@supabase/ui'
+import { Button, Typography, Listbox, IconUsers, IconAlertCircle } from '@supabase/ui'
import { API_URL } from 'lib/constants'
import { post } from 'lib/common/fetch'
@@ -23,9 +16,6 @@ import {
REGIONS,
REGIONS_DEFAULT,
DEFAULT_MINIMUM_PASSWORD_STRENGTH,
- PASSWORD_STRENGTH,
- PASSWORD_STRENGTH_COLOR,
- PASSWORD_STRENGTH_PERCENTAGE,
PRICING_PLANS,
PRICING_PLANS_DEFAULT,
DEFAULT_FREE_PROJECTS_LIMIT,
@@ -38,6 +28,8 @@ import { getURL } from 'lib/helpers'
import FormField from 'components/to-be-cleaned/forms/FormField'
import Panel from 'components/to-be-cleaned/Panel'
import InformationBox from 'components/ui/InformationBox'
+import { passwordStrength } from 'lib/helpers'
+import PasswordStrengthBar from 'components/ui/PasswordStrengthBar'
interface StripeCustomer {
paymentMethods: any
@@ -164,22 +156,10 @@ export const Wizard = observer(() => {
}
async function checkPasswordStrength(value: any) {
- let passwordStrength = ''
- if (value && value !== '') {
- const response = await post(`${API_URL}/profile/password-check`, { password: value })
- if (!response.error) {
- const { result } = response
- const score = (PASSWORD_STRENGTH as any)[result.score]
- const suggestions = result.feedback?.suggestions
- ? result.feedback.suggestions.join(' ')
- : ''
- passwordStrength = `${score} ${suggestions}`
- setPasswordStrengthScore(result.score)
- setPasswordStrengthWarning(result.feedback.warning ? result.feedback.warning : '')
- }
- }
-
- setPasswordStrengthMessage(passwordStrength)
+ const { message, warning, strength } = await passwordStrength(value)
+ setPasswordStrengthScore(strength)
+ setPasswordStrengthWarning(warning)
+ setPasswordStrengthMessage(message)
}
const onClickNext = async () => {
@@ -295,48 +275,13 @@ export const Wizard = observer(() => {
value={dbPass}
onChange={onDbPassChange}
description={
- <>
- {dbPass && (
- <div
- aria-valuemax={100}
- aria-valuemin={0}
- aria-valuenow={
- (PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore]
- }
- aria-valuetext={
- (PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore]
- }
- role="progressbar"
- className="mb-2 bg-bg-alt-light dark:bg-bg-alt-dark rounded overflow-hidden transition-all border dark:border-dark"
- >
- <div
- style={{
- width: (PASSWORD_STRENGTH_PERCENTAGE as any)[passwordStrengthScore],
- }}
- className={`relative h-2 w-full ${
- (PASSWORD_STRENGTH_COLOR as any)[passwordStrengthScore]
- } transition-all duration-500 ease-out shadow-inner`}
- ></div>
- </div>
- )}
- <span
- className={
- passwordStrengthScore >= DEFAULT_MINIMUM_PASSWORD_STRENGTH
- ? 'text-green-600'
- : ''
- }
- >
- {passwordStrengthMessage
- ? passwordStrengthMessage
- : 'This is the password to your postgres database, so it must be a strong password and hard to guess.'}
- </span>
- </>
- }
- errorMessage={
- passwordStrengthWarning
- ? `${passwordStrengthWarning}. ${passwordErrorMessage}.`
- : passwordErrorMessage
+ <PasswordStrengthBar
+ passwordStrengthScore={passwordStrengthScore}
+ password={dbPass}
+ passwordStrengthMessage={passwordStrengthMessage}
+ />
}
+ errorMessage={passwordStrengthWarning}
/>
</Panel.Content>
diff --git a/studio/pages/project/[ref]/settings/database.tsx b/studio/pages/project/[ref]/settings/database.tsx
index 03841ec2687..5efc721e4c8 100644
--- a/studio/pages/project/[ref]/settings/database.tsx
+++ b/studio/pages/project/[ref]/settings/database.tsx
@@ -1,25 +1,31 @@
-import useSWR from 'swr'
-import dayjs from 'dayjs'
-import customParseFormat from 'dayjs/plugin/customParseFormat'
-import timezone from 'dayjs/plugin/timezone'
-import utc from 'dayjs/plugin/utc'
-import { FC, useState } from 'react'
-import { isUndefined } from 'lodash'
+import { FC, useState, useRef } from 'react'
+import { useStore, withAuth } from 'hooks'
+
import { useRouter } from 'next/router'
+
import { toJS } from 'mobx'
import { observer } from 'mobx-react-lite'
-import { Typography, Input, Button, IconDownload, IconArrowRight, Tabs } from '@supabase/ui'
+import useSWR from 'swr'
-import { API_URL } from 'lib/constants'
-import { useStore, withAuth } from 'hooks'
+import { API_URL, DEFAULT_MINIMUM_PASSWORD_STRENGTH, TIME_PERIODS_INFRA } from 'lib/constants'
import { get } from 'lib/common/fetch'
-import { TIME_PERIODS_INFRA } from 'lib/constants'
-import { pluckObjectFields } from 'lib/helpers'
+import { pluckObjectFields, passwordStrength } from 'lib/helpers'
+
import { SettingsLayout } from 'components/layouts'
+import PasswordStrengthBar from 'components/ui/PasswordStrengthBar'
import Panel from 'components/to-be-cleaned/Panel'
+import { ProjectUsageMinimal } from 'components/to-be-cleaned/Usage'
import DateRangePicker from 'components/to-be-cleaned/DateRangePicker'
import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler'
-import { ProjectUsageMinimal } from 'components/to-be-cleaned/Usage'
+
+import { Typography, Input, Button, IconDownload, IconArrowRight, Tabs, Modal } from '@supabase/ui'
+
+import dayjs from 'dayjs'
+import customParseFormat from 'dayjs/plugin/customParseFormat'
+import timezone from 'dayjs/plugin/timezone'
+import utc from 'dayjs/plugin/utc'
+
+import { isUndefined, debounce } from 'lodash'
dayjs.extend(customParseFormat)
dayjs.extend(timezone)
@@ -135,6 +141,86 @@ const Usage: FC<any> = ({ project }) => {
)
}
+const ResetDbPassword: FC<any> = () => {
+ const [password, setPassword] = useState('')
+ const [passwordStrengthMessage, setPasswordStrengthMessage] = useState('')
+ const [passwordStrengthWarning, setPasswordStrengthWarning] = useState('')
+ const [passwordStrengthScore, setPasswordStrengthScore] = useState(0)
+
+ async function checkPasswordStrength(value: any) {
+ const { message, warning, strength } = await passwordStrength(value)
+ setPasswordStrengthScore(strength)
+ setPasswordStrengthWarning(warning)
+ setPasswordStrengthMessage(message)
+ }
+
+ const delayedCheckPasswordStrength = useRef(
+ debounce((value) => checkPasswordStrength(value), 300)
+ ).current
+
+ function onDbPassChange(e: any) {
+ const value = e.target.value
+ setPassword(value)
+ if (value == '') {
+ setPasswordStrengthScore(-1)
+ setPasswordStrengthMessage('')
+ } else delayedCheckPasswordStrength(value)
+ }
+
+ return (
+ <>
+ <Panel>
+ <Panel.Content>
+ <div className="grid grid-cols-1 lg:grid-cols-2 items-center">
+ <div>
+ <Typography.Text className="block">Database password</Typography.Text>
+ <div style={{ maxWidth: '420px' }}>
+ <Typography.Text type="secondary" className="opacity-50">
+ <p className="opacity-50">
+ You can use this password to connect directly to your Postgres database.
+ </p>
+ </Typography.Text>
+ </div>
+ </div>
+ <div className="flex items-end justify-end">
+ <Button type="default">Reset Database Password</Button>
+ </div>
+ </div>
+ </Panel.Content>
+ </Panel>
+ <Modal
+ title="Reset database password"
+ visible={true}
+ confirmText="Reset password"
+ alignFooter="right"
+ size="medium"
+ children={
+ <>
+ <div className="w-full space-y-8 mb-8">
+ <Input
+ type="password"
+ onChange={onDbPassChange}
+ label="New Password"
+ error={passwordStrengthWarning}
+ // @ts-ignore
+ descriptionText={
+ <>
+ <PasswordStrengthBar
+ passwordStrengthScore={passwordStrengthScore}
+ passwordStrengthMessage={passwordStrengthMessage}
+ password={password}
+ />
+ </>
+ }
+ />
+ </div>
+ </>
+ }
+ />
+ </>
+ )
+}
+
const DownloadCertificate: FC<any> = ({ createdAt }) => {
// instances before 3 : 08 pm sgt 29th April don't have certs installed
if (new Date(createdAt) < new Date('2021-04-30')) return null
@@ -142,10 +228,10 @@ const DownloadCertificate: FC<any> = ({ createdAt }) => {
return (
<Panel>
<Panel.Content>
- <div className="w-full flex items-center justify-between">
+ <div className="grid grid-cols-1 lg:grid-cols-2 items-center">
<div>
<Typography.Text className="block">SSL Connection</Typography.Text>
- <div style={{ maxWidth: '320px' }}>
+ <div style={{ maxWidth: '420px' }}>
<Typography.Text type="secondary" className="opacity-50">
<p className="opacity-50">
Use this cert when connecting to your database to prevent snooping and
@@ -154,13 +240,15 @@ const DownloadCertificate: FC<any> = ({ createdAt }) => {
</Typography.Text>
</div>
</div>
- <Button type="default" icon={<IconDownload />}>
- <a
- href={`https://supabase-downloads.s3-ap-southeast-1.amazonaws.com/${env}/ssl/${env}-ca-2021.crt`}
- >
- Download Certificate
- </a>
- </Button>
+ <div className="flex items-end justify-end">
+ <Button type="default" icon={<IconDownload />}>
+ <a
+ href={`https://supabase-downloads.s3-ap-southeast-1.amazonaws.com/${env}/ssl/${env}-ca-2021.crt`}
+ >
+ Download Certificate
+ </a>
+ </Button>
+ </div>
</div>
</Panel.Content>
</Panel>
@@ -264,6 +352,7 @@ const GeneralSettings: FC<any> = ({ projectRef }) => {
</Panel.Content>
</Panel>
</section>
+ <ResetDbPassword />
<DownloadCertificate createdAt={connectionInfo.inserted_at} />
</div>
<div>