Skip to content
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
8 changes: 4 additions & 4 deletions authentication/views/social.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.utils.module_loading import import_string
from rest_framework import serializers
from rest_framework import serializers, status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
Expand Down Expand Up @@ -37,7 +37,7 @@ def social_providers_api_view(request):


class SocialCodeAuth(SocialTokenOnlyAuthView):
def post(self, request, provider: str, *args, **kwargs):
# TODO: migrate social relations
def respond_error(self, error):
response = super().respond_error(error)

return super().post(request, provider, *args, **kwargs)
return Response({"detail": response.data}, status=status.HTTP_400_BAD_REQUEST)
20 changes: 20 additions & 0 deletions front_end/src/app/(main)/accounts/social/[provider]/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use server";

import AuthApi from "@/services/auth";
import { setServerSession } from "@/services/session";
import { SocialProviderType } from "@/types/auth";

export async function exchangeSocialOauthCode(
provider: SocialProviderType,
code: string
) {
const response = await AuthApi.exchangeSocialOauthCode(
provider,
code,
`${process.env.APP_URL}/accounts/social/${provider}`
);

if (response?.token) {
setServerSession(response.token);
}
}
37 changes: 37 additions & 0 deletions front_end/src/app/(main)/accounts/social/[provider]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use client";

import { useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";

import { exchangeSocialOauthCode } from "@/app/(main)/accounts/social/[provider]/actions";
import GlobalErrorBoundary from "@/components/global_error_boundary";
import LoadingIndicator from "@/components/ui/loading_indicator";
import { SocialProviderType } from "@/types/auth";
import { SearchParams } from "@/types/navigation";

export default function SocialAuth({
params: { provider },
searchParams,
}: {
params: { provider: SocialProviderType };
searchParams: SearchParams;
}) {
const [error, setError] = useState();
const router = useRouter();

useEffect(() => {
exchangeSocialOauthCode(provider, searchParams.code as string)
.then(() => router.push("/"))
.catch(setError);
}, [provider, searchParams.code, router]);

if (error) {
return <GlobalErrorBoundary error={error} reset={() => router.push("/")} />;
}

return (
<main className="mx-auto my-12 flex min-h-min w-full max-w-5xl flex-col gap-4 px-3 lg:px-0">
<LoadingIndicator className="mx-auto h-8 w-24 text-gray-600 dark:text-gray-600-dark" />
</main>
);
}
27 changes: 0 additions & 27 deletions front_end/src/app/(main)/accounts/social/[provider]/route.ts

This file was deleted.

15 changes: 5 additions & 10 deletions front_end/src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ class AuthApi {
code: string,
redirect_uri: string
): Promise<SocialAuthResponse | null> {
try {
return await post<
SocialAuthResponse,
{ code: string; redirect_uri: string }
>(`/auth/social/${provider}/`, {
return post<SocialAuthResponse, { code: string; redirect_uri: string }>(
`/auth/social/${provider}/`,
{
code,
redirect_uri,
});
} catch (err) {
console.error("Error getting social providers:", err);
return null;
}
}
);
}

static async signIn(login: string, password: string) {
Expand Down
1 change: 1 addition & 0 deletions metaculus_web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
# Google
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_KEY")
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get("SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET")
REST_SOCIAL_VERBOSE_ERRORS=True

# Email configuration
# https://anymail.dev/
Expand Down