File size: 13,723 Bytes
2f28b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""

Authentication Web Interface for OpenManus

Mobile number + password based authentication forms

"""

import asyncio
import sqlite3
from typing import Optional, Tuple

import gradio as gr

from app.auth import UserSignupRequest, UserLoginRequest
from app.auth_service import AuthService
from app.logger import logger


class AuthInterface:
    """Authentication interface with Gradio"""

    def __init__(self, db_path: str = "openmanus.db"):
        self.db_path = db_path
        self.auth_service = None
        self.current_session = None
        self.init_database()

    def init_database(self):
        """Initialize database with schema"""
        try:
            conn = sqlite3.connect(self.db_path)

            # Create users table with mobile auth
            conn.execute(
                """

                CREATE TABLE IF NOT EXISTS users (

                    id TEXT PRIMARY KEY,

                    mobile_number TEXT UNIQUE NOT NULL,

                    full_name TEXT NOT NULL,

                    password_hash TEXT NOT NULL,

                    avatar_url TEXT,

                    preferences TEXT,

                    is_active BOOLEAN DEFAULT TRUE,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

                )

            """
            )

            # Create sessions table
            conn.execute(
                """

                CREATE TABLE IF NOT EXISTS sessions (

                    id TEXT PRIMARY KEY,

                    user_id TEXT NOT NULL,

                    title TEXT,

                    metadata TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    expires_at DATETIME,

                    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE

                )

            """
            )

            conn.commit()
            conn.close()
            logger.info("Database initialized successfully")

        except Exception as e:
            logger.error(f"Database initialization error: {str(e)}")

    def get_db_connection(self):
        """Get database connection"""
        return sqlite3.connect(self.db_path)

    async def handle_signup(

        self, full_name: str, mobile_number: str, password: str, confirm_password: str

    ) -> Tuple[str, bool, dict]:
        """Handle user signup"""
        try:
            # Validate input
            if not all([full_name, mobile_number, password, confirm_password]):
                return "All fields are required", False, gr.update(visible=True)

            # Create signup request
            signup_data = UserSignupRequest(
                full_name=full_name,
                mobile_number=mobile_number,
                password=password,
                confirm_password=confirm_password,
            )

            # Process signup
            db_conn = self.get_db_connection()
            auth_service = AuthService(db_conn)

            result = await auth_service.register_user(signup_data)
            db_conn.close()

            if result.success:
                self.current_session = {
                    "session_id": result.session_id,
                    "user_id": result.user_id,
                    "full_name": result.full_name,
                }
                return (
                    f"Welcome {result.full_name}! Account created successfully.",
                    True,
                    gr.update(visible=False),
                )
            else:
                return result.message, False, gr.update(visible=True)

        except ValueError as e:
            return str(e), False, gr.update(visible=True)
        except Exception as e:
            logger.error(f"Signup error: {str(e)}")
            return "An error occurred during signup", False, gr.update(visible=True)

    async def handle_login(

        self, mobile_number: str, password: str

    ) -> Tuple[str, bool, dict]:
        """Handle user login"""
        try:
            # Validate input
            if not all([mobile_number, password]):
                return (
                    "Mobile number and password are required",
                    False,
                    gr.update(visible=True),
                )

            # Create login request
            login_data = UserLoginRequest(
                mobile_number=mobile_number, password=password
            )

            # Process login
            db_conn = self.get_db_connection()
            auth_service = AuthService(db_conn)

            result = await auth_service.login_user(login_data)
            db_conn.close()

            if result.success:
                self.current_session = {
                    "session_id": result.session_id,
                    "user_id": result.user_id,
                    "full_name": result.full_name,
                }
                return (
                    f"Welcome back, {result.full_name}!",
                    True,
                    gr.update(visible=False),
                )
            else:
                return result.message, False, gr.update(visible=True)

        except ValueError as e:
            return str(e), False, gr.update(visible=True)
        except Exception as e:
            logger.error(f"Login error: {str(e)}")
            return "An error occurred during login", False, gr.update(visible=True)

    def handle_logout(self) -> Tuple[str, bool, dict]:
        """Handle user logout"""
        if self.current_session:
            # In a real app, you'd delete the session from database
            self.current_session = None

        return "Logged out successfully", False, gr.update(visible=True)

    def create_interface(self) -> gr.Interface:
        """Create the authentication interface"""

        with gr.Blocks(
            title="OpenManus Authentication", theme=gr.themes.Soft()
        ) as auth_interface:
            gr.Markdown(
                """

            # πŸ” OpenManus Authentication

            ### Secure Mobile Number + Password Login System

            """
            )

            # Session status
            session_status = gr.Textbox(
                value="Not logged in", label="Status", interactive=False
            )

            # Auth forms container
            with gr.Column(visible=True) as auth_forms:

                with gr.Tabs():

                    # Login Tab
                    with gr.TabItem("πŸ”‘ Login"):
                        gr.Markdown("### Login with your mobile number and password")

                        login_mobile = gr.Textbox(
                            label="πŸ“± Mobile Number",
                            placeholder="Enter your mobile number (e.g., +1234567890)",
                            lines=1,
                        )

                        login_password = gr.Textbox(
                            label="πŸ”’ Password",
                            type="password",
                            placeholder="Enter your password",
                            lines=1,
                        )

                        login_btn = gr.Button("πŸ”‘ Login", variant="primary", size="lg")
                        login_result = gr.Textbox(label="Result", interactive=False)

                    # Signup Tab
                    with gr.TabItem("πŸ“ Sign Up"):
                        gr.Markdown("### Create your new account")

                        signup_fullname = gr.Textbox(
                            label="πŸ‘€ Full Name",
                            placeholder="Enter your full name",
                            lines=1,
                        )

                        signup_mobile = gr.Textbox(
                            label="πŸ“± Mobile Number",
                            placeholder="Enter your mobile number (e.g., +1234567890)",
                            lines=1,
                        )

                        signup_password = gr.Textbox(
                            label="πŸ”’ Password",
                            type="password",
                            placeholder="Create a strong password (min 8 chars, include uppercase, lowercase, digit)",
                            lines=1,
                        )

                        signup_confirm_password = gr.Textbox(
                            label="πŸ”’ Confirm Password",
                            type="password",
                            placeholder="Confirm your password",
                            lines=1,
                        )

                        signup_btn = gr.Button(
                            "πŸ“ Create Account", variant="primary", size="lg"
                        )
                        signup_result = gr.Textbox(label="Result", interactive=False)

            # Logged in section
            with gr.Column(visible=False) as logged_in_section:
                gr.Markdown("### βœ… You are logged in!")

                user_info = gr.Markdown("Welcome!")

                logout_btn = gr.Button("πŸšͺ Logout", variant="secondary")
                logout_result = gr.Textbox(label="Result", interactive=False)

            # Password requirements info
            with gr.Accordion("πŸ“‹ Password Requirements", open=False):
                gr.Markdown(
                    """

                **Password must contain:**

                - At least 8 characters

                - At least 1 uppercase letter (A-Z)

                - At least 1 lowercase letter (a-z)

                - At least 1 digit (0-9)

                - Maximum 128 characters



                **Mobile Number Format:**

                - 10-15 digits

                - Can include country code

                - Examples: +1234567890, 1234567890, +91987654321

                """
                )

            # Event handlers
            def sync_signup(*args):
                """Synchronous wrapper for signup"""
                return asyncio.run(self.handle_signup(*args))

            def sync_login(*args):
                """Synchronous wrapper for login"""
                return asyncio.run(self.handle_login(*args))

            def update_ui_after_auth(result_text, success, auth_forms_update):
                """Update UI after authentication"""
                if success:
                    return (
                        result_text,  # session_status
                        auth_forms_update,  # auth_forms visibility
                        gr.update(visible=True),  # logged_in_section visibility
                        f"### πŸ‘‹ {self.current_session['full_name'] if self.current_session else 'User'}",  # user_info
                    )
                else:
                    return (
                        "Not logged in",  # session_status
                        auth_forms_update,  # auth_forms visibility
                        gr.update(visible=False),  # logged_in_section visibility
                        "Welcome!",  # user_info
                    )

            def update_ui_after_logout(result_text, success, auth_forms_update):
                """Update UI after logout"""
                return (
                    "Not logged in",  # session_status
                    auth_forms_update,  # auth_forms visibility
                    gr.update(visible=False),  # logged_in_section visibility
                    "Welcome!",  # user_info
                )

            # Login button click
            login_btn.click(
                fn=sync_login,
                inputs=[login_mobile, login_password],
                outputs=[login_result, gr.State(), gr.State()],
            ).then(
                fn=update_ui_after_auth,
                inputs=[login_result, gr.State(), gr.State()],
                outputs=[session_status, auth_forms, logged_in_section, user_info],
            )

            # Signup button click
            signup_btn.click(
                fn=sync_signup,
                inputs=[
                    signup_fullname,
                    signup_mobile,
                    signup_password,
                    signup_confirm_password,
                ],
                outputs=[signup_result, gr.State(), gr.State()],
            ).then(
                fn=update_ui_after_auth,
                inputs=[signup_result, gr.State(), gr.State()],
                outputs=[session_status, auth_forms, logged_in_section, user_info],
            )

            # Logout button click
            logout_btn.click(
                fn=self.handle_logout, outputs=[logout_result, gr.State(), gr.State()]
            ).then(
                fn=update_ui_after_logout,
                inputs=[logout_result, gr.State(), gr.State()],
                outputs=[session_status, auth_forms, logged_in_section, user_info],
            )

        return auth_interface


# Standalone authentication app
def create_auth_app(db_path: str = "openmanus.db") -> gr.Interface:
    """Create standalone authentication app"""
    auth_interface = AuthInterface(db_path)
    return auth_interface.create_interface()


if __name__ == "__main__":
    # Run standalone auth interface for testing
    auth_app = create_auth_app()
    auth_app.launch(server_name="0.0.0.0", server_port=7860, share=False, debug=True)