feat: disable plex login if owner doesnt have plex linked

This commit is contained in:
Ryan Cohen
2022-09-14 20:00:54 +09:00
committed by sct
parent 6be30cdbed
commit 05bb8f8f9c
8 changed files with 59 additions and 41 deletions

View File

@@ -37,6 +37,7 @@ export interface PublicSettingsResponse {
locale: string;
emailEnabled: boolean;
newPlexLogin: boolean;
plexLoginEnabled: boolean;
}
export interface CacheItem {

View File

@@ -110,7 +110,7 @@ interface PublicSettings {
initialized: boolean;
}
interface FullPublicSettings extends PublicSettings {
export interface FullPublicSettings extends PublicSettings {
applicationTitle: string;
applicationUrl: string;
hideAvailable: boolean;
@@ -126,6 +126,7 @@ interface FullPublicSettings extends PublicSettings {
locale: string;
emailEnabled: boolean;
newPlexLogin: boolean;
plexLoginEnabled: boolean;
}
export interface NotificationAgentConfig {
@@ -512,6 +513,7 @@ class Settings {
locale: this.data.main.locale,
emailEnabled: this.data.notifications.agents.email.enabled,
newPlexLogin: this.data.main.newPlexLogin,
plexLoginEnabled: false,
};
}

View File

@@ -7,9 +7,10 @@ import type {
} from '@server/api/themoviedb/interfaces';
import { getRepository } from '@server/datasource';
import DiscoverSlider from '@server/entity/DiscoverSlider';
import { User } from '@server/entity/User';
import type { StatusResponse } from '@server/interfaces/api/settingsInterfaces';
import { Permission } from '@server/lib/permissions';
import { getSettings } from '@server/lib/settings';
import { getSettings, type FullPublicSettings } from '@server/lib/settings';
import logger from '@server/logger';
import { checkUser, isAuthenticated } from '@server/middleware/auth';
import { mapWatchProviderDetails } from '@server/models/common';
@@ -95,16 +96,23 @@ router.get('/status/appdata', (_req, res) => {
});
router.use('/user', isAuthenticated(), user);
router.get('/settings/public', async (req, res) => {
router.get<never, FullPublicSettings>('/settings/public', async (req, res) => {
const settings = getSettings();
const userRepository = getRepository(User);
const fullPublicSettings: FullPublicSettings = settings.fullPublicSettings;
if (!(req.user?.settings?.notificationTypes.webpush ?? true)) {
return res
.status(200)
.json({ ...settings.fullPublicSettings, enablePushRegistration: false });
} else {
return res.status(200).json(settings.fullPublicSettings);
fullPublicSettings.enablePushRegistration = false;
}
const admin = await userRepository.findOneBy({ id: 1 });
if (admin && admin.plexId) {
fullPublicSettings.plexLoginEnabled = true;
}
return res.status(200).json(fullPublicSettings);
});
router.get('/settings/discover', isAuthenticated(), async (_req, res) => {
const sliderRepository = getRepository(DiscoverSlider);

View File

@@ -26,7 +26,7 @@ const Login = () => {
const [error, setError] = useState('');
const [isProcessing, setProcessing] = useState(false);
const [authToken, setAuthToken] = useState<string | undefined>(undefined);
const { user, revalidate } = useUser();
const { revalidate } = useUser();
const router = useRouter();
const settings = useSettings();
@@ -40,7 +40,11 @@ const Login = () => {
const response = await axios.post('/api/v1/auth/plex', { authToken });
if (response.data?.id) {
revalidate();
const user = await revalidate();
if (user) {
router.push('/');
}
}
} catch (e) {
setError(e.response.data.message);
@@ -51,15 +55,7 @@ const Login = () => {
if (authToken) {
login();
}
}, [authToken, revalidate]);
// Effect that is triggered whenever `useUser`'s user changes. If we get a new
// valid user, we redirect the user to the home page as the login was successful.
useEffect(() => {
if (user) {
router.push('/');
}
}, [user, router]);
}, [authToken, revalidate, router]);
const { data: backdrops } = useSWR<string[]>('/api/v1/backdrops', {
refreshInterval: 0,
@@ -118,26 +114,30 @@ const Login = () => {
<Accordion single atLeastOne>
{({ openIndexes, handleClick, AccordionContent }) => (
<>
<button
className={`w-full cursor-default bg-gray-800 bg-opacity-70 py-2 text-center text-sm font-bold text-gray-400 transition-colors duration-200 focus:outline-none sm:rounded-t-lg ${
openIndexes.includes(0) && 'text-indigo-500'
} ${
settings.currentSettings.localLogin &&
'hover:cursor-pointer hover:bg-gray-700'
}`}
onClick={() => handleClick(0)}
disabled={!settings.currentSettings.localLogin}
>
{intl.formatMessage(messages.signinwithplex)}
</button>
<AccordionContent isOpen={openIndexes.includes(0)}>
<div className="px-10 py-8">
<PlexLoginButton
isProcessing={isProcessing}
onAuthToken={(authToken) => setAuthToken(authToken)}
/>
</div>
</AccordionContent>
{settings.currentSettings.plexLoginEnabled && (
<>
<button
className={`w-full cursor-default bg-gray-800 bg-opacity-70 py-2 text-center text-sm font-bold text-gray-400 transition-colors duration-200 focus:outline-none sm:rounded-t-lg ${
openIndexes.includes(0) && 'text-indigo-500'
} ${
settings.currentSettings.localLogin &&
'hover:cursor-pointer hover:bg-gray-700'
}`}
onClick={() => handleClick(0)}
disabled={!settings.currentSettings.localLogin}
>
{intl.formatMessage(messages.signinwithplex)}
</button>
<AccordionContent isOpen={openIndexes.includes(0)}>
<div className="px-10 py-8">
<PlexLoginButton
isProcessing={isProcessing}
onAuthToken={(authToken) => setAuthToken(authToken)}
/>
</div>
</AccordionContent>
</>
)}
{settings.currentSettings.localLogin && (
<div>
<button

View File

@@ -213,7 +213,11 @@ const UserGeneralSettings = () => {
</label>
<div className="flex items-center rounded sm:col-span-2">
<div className="mr-4 flex h-7 w-7 items-center justify-center rounded-full border border-gray-700 bg-gray-800">
<CheckCircleIcon className="w-full text-green-500" />
<CheckCircleIcon
className={`w-full ${
user?.isPlexUser ? 'text-green-500' : 'text-gray-700'
}`}
/>
</div>
<PlexLogo className="h-8 border-r border-gray-700 pr-4" />
{!user?.isPlexUser ? (
@@ -223,6 +227,7 @@ const UserGeneralSettings = () => {
onComplete={() => {
revalidateUser();
}}
textOverride="Connect Plex Account"
/>
</div>
</>

View File

@@ -24,6 +24,7 @@ const defaultSettings = {
locale: 'en',
emailEnabled: false,
newPlexLogin: true,
plexLoginEnabled: false,
};
export const SettingsContext = React.createContext<SettingsContextProps>({

View File

@@ -9,7 +9,7 @@ export type { PermissionCheckOptions };
export interface User {
id: number;
plexUsername?: string;
plexUsername?: string | null;
username?: string;
displayName: string;
email: string;

View File

@@ -228,6 +228,7 @@ CoreApp.getInitialProps = async (initialProps) => {
locale: 'en',
emailEnabled: false,
newPlexLogin: true,
plexLoginEnabled: false,
};
if (ctx.res) {